我想计算LRC(在JAVA中)以获取以下消息:
String TLV1="003D6000010000544553543531333030303234FF8B6028DF0225DF82040C544553543531333030303234DF820803000160DF820903170222DF820A03172011D5";
我的LRC应该是“D5”= 213但我的代码计算232 :(我不知道我在哪里做错了。
public short calculateLRC()
{
short LRC = 0;
for (int i =2 ; i < TLV1.length()-2; i=i+2)
{
String a1=TLV1.substring(i,i+2);
String a2="0x"+a1;
Integer a3 = Integer.decode(a2);
int a4[] = new int [TLV1.length()];
a4[i/2]=a3;
LRC ^= a4[i/2];
}
return LRC;
}
答案 0 :(得分:0)
更改您的代码
LRC ^= TLV[i]; //Says array required but string found
到
LRC ^= (byte)TLV.charAt(i);