当前位置:网站首页>internship:普通常用的工具类编写
internship:普通常用的工具类编写
2022-07-25 22:08:00 【ahyo】
以下是工具类里的一些编码转换方法
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class CommonUtil {
/** * Ascii转换为字符串 * * @param value * @return */
public static String asciiToString(String value) {
try {
if (value == null || "".equalsIgnoreCase(value)) {
return null;
}
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(",");
for (String aChar : chars) {
sbu.append((char) Integer.parseInt(aChar.trim()));
}
return sbu.toString();
} catch (Exception e) {
log.info("resolve data: " + value + " error!" + e + e.getMessage());
}
return null;
}
/** * 16进制转为10进制转为字符 * * @param str * @return example: $PTNL, PJK, 022330.00, 052018,+3818626.239,N,+504060.470,E,3,20,1.3,EHT+67.967,M*75 */
public static String hexToStr(String str) {
if (str.isEmpty() || str.length() < 5) {
return null;
}
try {
str = str.replaceAll("[\r\n]*", "");
List<String> list = new ArrayList<>();
String[] strArr = str.split("");
for (int i = 0; i < strArr.length - 1; i += 2) {
//拆分并转成10进制
list.add(Integer.parseInt(strArr[i] + strArr[i + 1], 16) + "");
}
String tempStr = list.toString();
String tempStr2 = tempStr.substring(1, tempStr.length() - 1);
String result = asciiToString(tempStr2).replaceAll("[\r\n]*", "");
return result;
} catch (Exception e) {
log.info("resolve data error!" + e + e.getMessage());
}
return null;
}
/** * 中文转unicode编码 */
public static String convertUnicode(final String string) {
if (StringUtils.isEmpty(string)) {
return null;
}
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
unicode.append("\\u" + Integer.toHexString(c));
}
return unicode.toString();
}
/** * unicode编码转中文 */
public static String decodeUnicode(final String dataStr) {
String[] strArr = dataStr.split("\\\\u");
String returnStr = "";
for (int i = 1; i < strArr.length; i++) {
returnStr += (char) Integer.valueOf(strArr[i], 16).intValue();
}
return returnStr;
}
/** * 关闭资源/连接,注意关闭顺序 * * @param objects */
public static boolean close(Object... objects) throws Exception {
if (objects == null || objects.length < 1) {
return false;
}
for (Object object : objects) {
if (object == null) {
continue;
}
if (object instanceof Connection) {
((Connection) object).close();
}
if (object instanceof PreparedStatement) {
((PreparedStatement) object).close();
}
if (object instanceof ResultSet) {
((ResultSet) object).close();
}
if (object instanceof Socket) {
((Socket) object).close();
}
if (object instanceof FileReader) {
((FileReader) object).close();
}
if (object instanceof FileWriter) {
((FileWriter) object).close();
}
if (object instanceof FileInputStream) {
((FileInputStream) object).close();
}
if (object instanceof FileOutputStream) {
((FileOutputStream) object).close();
}
if (object instanceof BufferedReader) {
((BufferedReader) object).close();
}
if (object instanceof BufferedWriter) {
((BufferedWriter) object).close();
}
if (object instanceof BufferedInputStream) {
((BufferedInputStream) object).close();
}
if (object instanceof BufferedOutputStream) {
((BufferedOutputStream) object).close();
}
}
return false;
}
/** * 计算字符串的crc校验码 * * @param data * @return */
public static String getStrCrc(String data) {
data = data.replace(" ", "");
int len = data.length();
if (len % 2 != 0) {
return "0000";
}
int num = len / 2;
byte[] para = new byte[num];
for (int i = 0; i < num; i++) {
int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
para[i] = (byte) value;
}
return getCrc(para);
}
/** * 计算字节数组的crc校验码 * * @param bytes * @return */
public static String getCrc(byte[] bytes) {
//CRC寄存器全为1
int CRC = 0x0000ffff;
//多项式校验值
int POLYNOMIAL = 0x0000a001;
int i, j;
for (i = 0; i < bytes.length; i++) {
CRC ^= ((int) bytes[i] & 0x000000ff);
for (j = 0; j < 8; j++) {
if ((CRC & 0x00000001) != 0) {
CRC >>= 1;
CRC ^= POLYNOMIAL;
} else {
CRC >>= 1;
}
}
}
//结果转换为16进制
String result = Integer.toHexString(CRC).toUpperCase();
if (result.length() != 4) {
StringBuffer sb = new StringBuffer("0000");
result = sb.replace(4 - result.length(), 4, result).toString();
}
//交换高低位
return result;
}
/** * 16进制字符串转为byte数组 * * @param hexStr * @return */
public static byte[] hexStrToByteArray(String hexStr) {
byte[] arr = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() - 1; i += 2) {
int a = Integer.valueOf(hexStr.substring(i, i + 2), 16);
arr[i / 2] = (byte) a;
}
return arr;
}
public static byte[] int2Bytes(int integer) {
byte[] bytes = new byte[4];
bytes[3] = (byte) (integer >> 24);
bytes[2] = (byte) (integer >> 16);
bytes[1] = (byte) (integer >> 8);
bytes[0] = (byte) integer;
return bytes;
}
}
边栏推荐
- Jmeter--- set proxy recording request
- 若依如何解决导出使用下载插件出现异常?
- 6-18漏洞利用-后门连接
- YUV420 YUV420sp 图像格式「建议收藏」
- 文件无法保存(文件夹已损坏无法读取怎么办)
- LeetCode_ 93_ Restore IP address
- 卸载npm和安装npm_使用`npm uninstall`卸载npm软件包「建议收藏」
- The testing work is not valued. Have you changed your position?
- Unity performance optimization direction
- At present, flynk CDC does not support mysql5.5. If you change the source code and release this restriction, there will be a lot of data problems?
猜你喜欢
随机推荐
What have I experienced to become a harder tester than development?
如何实现一个App应用程序,限制用户时间使用?
[test development methodology] experience of test development platform PK - choice
【饭谈】软件测试薪资层次和分段(修仙)
How to use RS485 half duplex chip correctly
JS timer and swiper plug-in
C language left value and right value description [easy to understand]
Dovecot set mailbox quota
SQL中in的用法 DQL 查询
Playwright tutorial (II) suitable for Xiaobai
JSP初识
Nuclear power plants strive to maintain safety in the heat wave sweeping Europe
What is partition and barrel division?
How is it most convenient to open an account for stock speculation? Is it safe for online account managers to open an account
C language: random generated number + bubble sort
At present, flynk CDC does not support mysql5.5. If you change the source code and release this restriction, there will be a lot of data problems?
MySQL - subquery - column subquery (multi row subquery)
The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
sql语句练习题整理
Open source RSS subscriber freshrss



![[hand tear STL] BitSet (bitmap), bloom filter](/img/bc/a6e3c2ccce478460a2742852eee9cf.png)





