写在前面
我从事消防物联行业,负责对接过各种物联网设备。通俗地讲,在这些设备中有比较传统的,我们采集到的这类设备的原始数据一般都是一串十六进制的数字,两个数字作为一个字节,然后根据已有协议对其进行解析。还有一类比较先进的,例如来自电信物联网平台的,拿到的数据直接就是json格式的,这类数据一般已经表达得很清晰了,参考给定协议很快就能解析出来各个字段。
由于使用java语言进行解析,对于传统设备,在此分享解析中遇到的一些比较实用的工具类;对于json格式的数据,大家可以使用各种json工具包进行快速解析。
进制转换
- 十六进制转带符号的十进制(注意代码中的"0x80",如果十六进制字符串长度为2,就用"0x80",需减去256(255 + 1);如果十六进制字符串长度为4,就用"0x8000",需减去65536(65535 + 1);······,以此类推)
/** * 十六进制转为十进制 * @param strHex 要转换的十六进制字符串 * @return */ public static int hexToDec(String strHex) { int i = new BigInteger(strHex, 16).intValue(); // 判断其最高位是否为1,如果是1,则为负数,需要重新转换为十进制负数 if ((i & 0x80) != 0) { i = i - 256; } return i; }
- 十进制转十六进制(“%08X”的含义:8表示8位,0表示如果不够8位则往前面补0,X是大写,x是小写。大家可根据需要自行替换)
/** * 十进制转为十六进制 * @param intDec 要转换的十进制数 * @return */ public static String decToHex(int intDec) { return String.format("%08X", intDec); }
byte数组与十六进制字符串(jdk中提供了方法)
- byte数组转十六进制字符串
/** * byte数组转十六进制字符串 * @param bytes 要转换的byte数组 * @return */ public static String bytesToHexStr(byte[] bytes) { //HexBinaryAdapter的全类名为:javax.xml.bind.annotation.adapters.HexBinaryAdapter HexBinaryAdapter hexBinaryAdapter = new HexBinaryAdapter(); return hexBinaryAdapter.marshal(bytes); }
- 十六进制字符串转byte数组
/** * 十六进制字符串转byte数组 * @param hexStr 要转换的十六进制字符串 * @return */ public static byte[] hexStrToBytes(String hexStr) { //HexBinaryAdapter的全类名为:javax.xml.bind.annotation.adapters.HexBinaryAdapter HexBinaryAdapter hexBinaryAdapter = new HexBinaryAdapter(); return hexBinaryAdapter.unmarshal(hexStr); }
byte数组与base64编码(jdk中提供了方法)
- byte数组转base64编码字符串
/** * byte数组转base64编码字符串 * @param bytes 要转换的byte数组 * @return */ public static String bytesToBase64Str(byte[] bytes) { //DatatypeConverter的全类名为:javax.xml.bind.DatatypeConverter return DatatypeConverter.printBase64Binary(bytes); }
- base64编码字符串转byte数组
/** * base64编码字符串转byte数组 * @param base64Str 要转换的base64字符串 * @return */ public static byte[] base64StrToBytes(String base64Str) { //DatatypeConverter的全类名为:javax.xml.bind.DatatypeConverter return DatatypeConverter.parseBase64Binary(base64Str); }
byte数组与真实数据的转换
一般在报文中,有的是两个字节代表设备的某个值,有的是四个字节代表设备的某个值,还有的是八个字节代表设备的某个值,都需要经过咱们的转换才能得到真实值。转换的过程还不尽相同,有的比较简单,直接十六进制转十进制就可以了;有的则需要先转换大端模式或小端模式再进行进制转换;有复杂的则需要进行一系列运算才能得到真实数据。我把我遇到的写在下面,希望大家也能用上。
- 针对两个字节的,将其转换为byte数组,再使用以下方法转为short类型的数据
public static short bytesToShort(byte[] bytes) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); short s = dis.readShort(); dis.close(); return s; }
- 针对四个字节的,将其转换为byte数组,再使用以下方法转为float类型的数据
public static float bytesToFloat(byte[] bytes) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); float f = dis.readFloat(); dis.close(); return f; }
- 针对八个字节的,将其转换为byte数组,再使用以下方法转为double类型的数据
public static double bytesToDouble(byte[] bytes) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); double d = dis.readDouble(); dis.close(); return d; }
大端模式与小端模式
大端模式中,字数据的高字节存储在低地址中,而字数据的低字节则存放在高地址中;在小端模式中,低地址中存放的是字数据的低字节,高地址存放的是字数据的高字节。如果需要转换,则使用以下方法将byte数组元素进行翻转。
/**
* byte数组翻转
* @param arr
*/
public byte[] reverse(byte[] arr){
//遍历数组
for(int i = 0; i < arr.length / 2; i++){
//交换元素,因为i从0开始所以这里一定要再减去1
byte temp = arr[arr.length - i - 1];
arr[arr.length - i - 1] = arr[i];
arr[i] = temp;
}
//返回翻转后的结果
return arr;
}
校验和的计算
- 最原始的校验和计算(实质就是将报文中每个字节都相加,这里返回两个字节的结果)
/** * 原始校验和 * @param hexStr 要计算校验和的报文的字符串形式 * @return */ public static String makeCheckSum(String hexStr) { if (hexStr == null || hexStr.equals("")) { return "00"; } hexStr = hexStr.replaceAll(" ", ""); int total = 0; int len = hexStr.length(); if (len % 2 != 0) { return "00"; } int num = 0; while (num < len) { String s = hexStr.substring(num, num + 2); total += Integer.parseInt(s, 16); num = num + 2; } return hexInt(total); } public static String hexInt(int total) { int a = total / 256; int b = total % 256; if (a > 255) { return hexInt(a) + format(b); } return format(a) + format(b); } public static String format(int hex) { String hexa = Integer.toHexString(hex); int len = hexa.length(); if (len < 2) { hexa = "0" + hexa; } return hexa; }
- 另一种校验码的计算:就是在第1种结果的基础上进行以下运算:0x100 - sum(bytes[len]) & 0xff("%02X"返回一个字节,有需要可自行更改)
/** * 计算校验码 * @param hexStr 要计算校验码的报文的字符串形式 * @return */ public static String makeCheckCode(String hexStr) { if (hexStr == null || hexStr.equals("")) { return "00"; } hexStr = hexStr.replaceAll(" ", ""); int total = 0; int len = hexStr.length(); if (len % 2 != 0) { return "00"; } int num = 0; while (num < len) { String s = hexStr.substring(num, num + 2); total += Integer.parseInt(s, 16); num = num + 2; } int i = 0x100 - total & 0xff; return String.format("%02X", i); }
- 二进制反码求和(这里传入和返回的均为byte数组,如果需要转换为十六进制字符串,参照我上面的方法即可)
/** * 二进制反码求和 * @param bytes * @return 一字节校验和 */ public static byte[] sumBinInverseCode(byte[] bytes) { int mSum = 0; for (byte byteMsg : bytes) { int mNum = (byteMsg >= 0) ? byteMsg : (byteMsg + 256); mSum += mNum; } int sum = (mSum >> 8) + (mSum & 0xff); String s = Integer.toHexString(~sum); int length = s.length(); return new HexBinaryAdapter().unmarshal(s.substring(length-2, length)); }
- CRC16校验码(一般用在像MODBUS这样一问一答的协议中)
/** * 计算CRC16校验码 * @param hexStr 要计算CRC16校验码的报文的字符串形式 * @return */ public static String getCRC(String hexStr) { hexStr = hexStr.replace(" ", ""); int len = hexStr.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(hexStr.substring(i * 2, 2 * (i + 1)), 16); para[i] = (byte) value; } return getCRC(para); } /** * 计算CRC16校验码 * @param bytes 要计算CRC16校验码的报文的byte数组形式 * @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.substring(2, 4) + result.substring(0, 2); }
结尾
此文章内容会不断丰富,如有错误,请大家别客气,直接在评论区指正即可!