1078-优化BCD码解析

This commit is contained in:
648540858
2024-03-16 23:41:29 +08:00
parent 3e672ca38c
commit 98199ec657
3 changed files with 61 additions and 56 deletions

View File

@@ -1,17 +1,22 @@
package com.genersoft.iot.vmp.jt1078.util;
/**
* BCD码转换
*/
public class BCDUtil {
public static String transform(byte[] bytes) {
if (bytes.length == 0) {
return null;
}
// BCD[6] 解析时间
StringBuffer temp = new StringBuffer(bytes.length * 2);
// BCD
StringBuilder stringBuffer = new StringBuilder(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
temp.append((byte) (bytes[i] & 0x0f));
// 每次取出四位的值一个byte是八位第一取出高四位第二次取出低四位
// 这里也可以先 & 0xf0再右移4位0xf0二进制为11110000与运算后可以得到高4位是值低四位清零的结果
stringBuffer.append((byte) ((bytes[i] >>> 4 & 0xf)));
stringBuffer.append((byte) (bytes[i] & 0x0f));
}
return temp.toString();
return stringBuffer.toString();
}
}