package com.genersoft.iot.vmp.netty.util; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; public class HexStringTool { private static final char[] HEX_CHAR_ARR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private static final String HEX_STR = "0123456789abcdef"; //对字符串,进行分组,转化为List public static List getStrList(String str16, int frontLen, int size, int length) { List list = new ArrayList(); String content = str16.substring(frontLen); String tem = ""; for (int index = 0; index < size; index++) { tem = content.substring(0, length); content = content.substring(length); list.add(tem); } return list; } //对字符串,进行分组,转化为List(字符串数据类型转化专用) public static List getStrList(String str16, int frontLen, int size, int totalLen, int lenLen) { List list = new ArrayList<>(); String content = str16.substring(frontLen); String tem = ""; for (int index = 0; index < size; index++) { String len16 = content.substring(totalLen * 2, (totalLen + lenLen) * 2); int len = Integer.parseInt(len16, 16); int length = (totalLen + lenLen + len) * 2; tem = content.substring(0, length); content = content.substring(length); list.add(tem); } return list; } //字符串转换unicode public static String string2Unicode(String string) { StringBuffer unicode = new StringBuffer(); for (int i = 0; i < string.length(); i++) { // 取出每一个字符 char c = string.charAt(i); // 转换为unicode unicode.append("\\u" + Integer.toHexString(c)); } return unicode.toString(); } //字符串转化成为16进制字符串 public static String strTo16(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { int ch = (int) s.charAt(i); String s4 = Integer.toHexString(ch); str = str + s4; } return str; } //16进制转换成为string类型字符串 public static String hexStringToString(String s) { if (s == null || s.equals("")) { return null; } s = s.replace(" ", ""); byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "UTF-8"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } //unicode 转字符串 public static String unicode2String(String unicode) { StringBuffer string = new StringBuffer(); String[] hex = unicode.split("\\\\u"); for (int i = 1; i < hex.length; i++) { // 转换出每一个代码点 int data = Integer.parseInt(hex[i], 16); // 追加成string string.append((char) data); } return string.toString(); } //字符串转换成为16进制(无需Unicode编码) public static String str2HexStr(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); // sb.append(' '); } return sb.toString().trim(); } //16进制直接转换成为字符串(无需Unicode解码) public static String hexStr2Str(String hexStr) { String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return new String(bytes); } /** * int/long转16字节字符串(按所占字节数用0补齐) * * @param byteLen 所占字节长度 * @param b 需要转换的数字 * @return str16 */ public static String numToStr16(int byteLen, long b) { return String.format("%0" + byteLen * 2 + "x", b); } /** * float/double转16字节字符串(按所占字节数用0补齐) * * @param byteLen 所占字节长度 * @param b 需要转换的数字 * @return str16 */ public static String numToStr16(int byteLen, double b) { byte[] bytes = Hex.getBytes(b); int anInt = Hex.getInt(bytes); return String.format("%0" + byteLen * 2 + "x", anInt); } /** * 16进制字符串转字符串(ASCII) * * @param x16 十六进制字符串 * @return string */ public static String x16toString(String x16) { if (x16 == null || "".equals(x16.trim())) { return ""; } String tempStr = ""; byte[] b = new byte[x16.length() / 2]; for (int i = 0; i < x16.length() / 2; i++) { tempStr = x16.substring(i * 2, i * 2 + 2); int temp = Integer.parseInt(tempStr, 16); b[i] = (byte) temp; } return new String(b, StandardCharsets.US_ASCII); } /** * 16进制字符串转字符串(GBK) * * @param x16 十六进制字符串 * @return string */ public static String x16toGbk(String x16) { byte[] bytes = new byte[x16.length() / 2]; byte tempByte = 0; byte tempHigh = 0; byte tempLow = 0; for (int i = 0, j = 0; i < x16.length(); i += 2, j++) { tempByte = (byte) (((int) x16.charAt(i)) & 0xff); if (tempByte >= 48 && tempByte <= 57) { tempHigh = (byte) ((tempByte - 48) << 4); } else if (tempByte >= 97 && tempByte <= 101) { tempHigh = (byte) ((tempByte - 97 + 10) << 4); } tempByte = (byte) (((int) x16.charAt(i + 1)) & 0xff); if (tempByte >= 48 && tempByte <= 57) { tempLow = (byte) (tempByte - 48); } else if (tempByte >= 97 && tempByte <= 101) { tempLow = (byte) (tempByte - 97 + 10); } bytes[j] = (byte) (tempHigh | tempLow); } String result = null; try { result = new String(bytes, "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } public static boolean isChinese(String s) { boolean result = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { result = true; } } return result; } private static String byteArrToHex(byte[] btArr) { char[] strArr = new char[btArr.length * 2]; int i = 0; for (byte bt : btArr) { strArr[i++] = HEX_CHAR_ARR[bt >>> 4 & 0xf]; strArr[i++] = HEX_CHAR_ARR[bt & 0xf]; } return new String(strArr); } private static byte[] hexToByteArr(String hexStr) { char[] charArr = hexStr.toCharArray(); byte[] btArr = new byte[charArr.length / 2]; int index = 0; for (int i = 0; i < charArr.length; i++) { int highBit = HEX_STR.indexOf(charArr[i]); int lowBit = HEX_STR.indexOf(charArr[++i]); btArr[index] = (byte) (highBit << 4 | lowBit); index++; } return btArr; } /** * 字符串转16进制字符串(无汉字utf-8,有汉字gbk) * * @param str * @return */ public static String str2StrHex(String str) { boolean chinese = isChinese(str); if (chinese) { try { byte[] bytes1 = str.getBytes(StandardCharsets.UTF_8); byte[] bytes2 = new String(bytes1, StandardCharsets.UTF_8).getBytes("gbk"); String gbk = new String(bytes2, "gbk"); byte[] gbkArr = gbk.getBytes(); return byteArrToHex(gbkArr); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } else { return strTo16(str); } } /** * 16进制字符串转字符串(无汉字utf-8,有汉字gbk) * * @param strHex * @return */ public static String strHex2Str(String strHex) { byte[] bytes = hexToByteArr(strHex); String s1 = new String(bytes, StandardCharsets.UTF_8); boolean chinese = isChinese(s1); if (!chinese) { try { return new String(bytes, "gbk"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } else { return s1; } } }