发现错误的LRC

时间:2017-02-27 07:25:38

标签: java android output tlv .lrc

我想计算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;

}

1 个答案:

答案 0 :(得分:0)

更改您的代码

LRC ^= TLV[i]; //Says array required but string found

LRC ^= (byte)TLV.charAt(i);