0

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;

}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Akin Gunduz
  • 103
  • 7
  • Are you speaking of the [Longitudinal Redundancy Check](https://en.wikipedia.org/wiki/Longitudinal_redundancy_check)? What's the problem you're encountering more specifically? The more background you can provide, the easier it is for folks to answer. – Horia Coman Feb 27 '17 at 07:29
  • Duplicate : http://stackoverflow.com/questions/6221886/calculating-lrc-in-java – Radi Feb 27 '17 at 07:30
  • 1
    Possible duplicate of [Calculating LRC in java](http://stackoverflow.com/questions/6221886/calculating-lrc-in-java) – Radi Feb 27 '17 at 07:30
  • 1
    This "question" (in fact, ther is no question) lacks many things: a detailed problem description, code that has been tried, research (there are some obvious duplicates), and others. But yet someone upvoted? – Seelenvirtuose Feb 27 '17 at 07:38

1 Answers1

1

change your code

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

to

LRC ^= (byte)TLV.charAt(i);
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117