javaandroidoutputtlv

Found wrong LRC


I want to calculate LRC (in JAVA) for the following message:

String TLV1="003D6000010000544553543531333030303234FF8B6028DF0225DF82040C544553543531333030303234DF820803000160DF820903170222DF820A03172011D5";

My LRC should be "D5" = 213 But my code calculate 232 :( I do not know where I'm doing wrong.

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;

}

Solution

  • change your code

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

    to

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