这样需要注意的是每两个字符互换了,我这里的原因是modbus tcp通讯读取出来的数据需要互换
private static Object instFloatStringData(byte[] response, String type) {
byte[] stringData = new byte[response.length * 2];
int char_len = 0; //定义字符长度变量,该数值为字符的长度。
for (int i = 0; response[10 + i] > 0; i = i + 2) { //循环判断每个字符,遇到00则说明字符已完,后面为空,同时计算字符长度。
stringData[i] = response[10 + i];
stringData[i + 1] = response[9 + i];
if (response[9 + i] > 0) {
char_len = i + 2;
} else {
char_len = i + 1;
}
}
//按照刚才循环的结果,将临时数组里的字符转至新数组temp1,新数组的长度刚好是字符的长度
byte[] temp1 = new byte[char_len];
System.arraycopy(stringData, 0, temp1, 0, char_len);
return new String(temp1);
}