出于项目原因接触了灯控板相关的设备,设备中发送指定用到了累加和校验,虽然有厂商提供的c#算法,也自认为完美移植到了java上,可就是结果不对,仔细看了看才发现原因:
c#的byte边界值为:0~255,
java的byte边界值为:-127~128,于是写了个简易的转换方法,如下:
String s="helloWorld";
byte[] b = s.getBytes();// 按照指定编码将String编程字节数组
int total = 0;
int temp;
// java模拟c# byte
for (int i = 0; i < b.length; i++) {
temp = b[i] & 0xff;
if (total + temp <= 0xff) {
total += temp;
} else {
total = total + temp - 1 - 0xff;
}
}