byte[] bytes = Hex16StringToHex16Byte(“A5 01 01”);
// 此方法用于将16进制的字符串转换成16进制的字节数组 public static byte[] Hex16StringToHex16Byte(String _hex16String) { //去掉字符串中的空格。 _hex16String = _hex16String.replace(" ", ""); if (_hex16String.length() / 2 == 0) { _hex16String += " "; } //声明一个字节数组,其长度等于字符串长度的一半。 byte[] buffer = new byte[_hex16String.length() / 2]; for (int i = 0; i < buffer.length; i++) { //为字节数组的元素赋值。 buffer[i] = (byte) Integer.parseInt((_hex16String.substring(i * 2, (i*2)+2)), 16); Log.e("bytes","bytes["+i+"]==="+buffer[i]+";================_hex16String"+_hex16String+"****"+_hex16String.substring(i * 2, (i*2)+2)); } //返回字节数组 return buffer; }
String string = Hex16ByteToHex16String(bytes); // 此方法用于将16进制的字节数组转换成16进制的字符串 public static String Hex16ByteToHex16String(byte[] _hex16Byte) { String result = ""; //如果字节数组不为空。 if (_hex16Byte != null) { for (int i = 0; i < _hex16Byte.length; i++) { //将每一个字节数组转换成16进制string类型的字符串,用空格分隔开。 String s = Integer.toHexString(0xFF & _hex16Byte[i]) + ""; if (s.length() == 1) { //sb.append('0'); result +="0"; } result += s+" "; } } Log.e("ffff", "16ByteToHex16String1: ==="+result+"===" ); return result; }