hex byte[]及String之间的互转,在进行ASCII、unicode转中文或数字时可能会用到,底层通信时常常会使用hex byte[]进行传输,但是显示时往往需要转换ASCII、unicode等。
这部分内容来自:https://blog.csdn.net/sdjkjsdh/article/details/84940702
public class StringToHex { /** * 字符串转换为16进制字符串 * * @param s * @return */ public static String stringToHexString(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { int ch = s.charAt(i); String s4 = Integer.toHexString(ch); str = str + s4; } return str; } /** * 16进制字符串转换为字符串 * * @param s * @return */ 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, "gbk"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } /** * 16进制表示的字符串转换为字节数组 * * @param s 16进制表示的字符串 * @return byte[] 字节数组 */ public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] b = new byte[len / 2]; for (int i = 0; i < len; i += 2) { // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节 b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character .digit(s.charAt(i + 1), 16)); } return b; } /** * byte数组转16进制字符串 * @param bArray * @return */ public static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); String sTemp; for (int i = 0; i < bArray.length; i++) { sTemp = Integer.toHexString(0xFF & bArray[i]); if (sTemp.length() < 2) sb.append(0); sb.append(sTemp.toUpperCase()); } return sb.toString(); } }
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); private static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) public static String UnicodeByteToStr(byte[] bBuf) { return new String(bBuf, StandardCharsets.UTF_16LE); // 这种不会处理字符串结束符 \0 } public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] b = new byte[len / 2]; for (int i = 0; i < len; i += 2) { // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节 b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character .digit(s.charAt(i + 1), 16)); } return b; } //看下是转ASCII码还是转Unicode //比如0x3031转为01 String effectiveDateRes = new String(effectiveDateHexStr, 0, 16, StandardCharsets.US_ASCII); //比如0x8b73转为中文'王' //当8、b、7、3使用4个字节的ASCII分别存储传输时则需要先将底层串口通信数据转为string(“8b73”),再通过hexStringToByteArray转为byte[],再通过下面的方式转成中文 String issuingAuthorityRes = UnicodeByteToStr(issuingAuthorityHexStr);
重点:
//看下是转ASCII码还是转Unicode
//比如0x3031转为01
String effectiveDateRes = new String(effectiveDateHexStr, 0, 16, StandardCharsets.US_ASCII);
//比如0x8b73转为中文’王’
//当8、b、7、3使用4个字节的ASCII分别存储传输时则需要先将底层串口通信数据转为string(“8b73”),再通过hexStringToByteArray转为byte[],再通过下面的方式转成中文
String issuingAuthorityRes = UnicodeByteToStr(issuingAuthorityHexStr);