Search code examples
javaarraysbytechecksumsecs

Getting the two bytes by adding the 16 bit unsigned of the byte array as checksum


I have this array of bytes that I want to calculate to get a 2 byte checksum.

byte[] b = {(byte) 0x80, (byte) 0x00, (byte) 0x81, (byte) 0x01, (byte) 0x80, (byte) 0x01,(byte) 0x00, (byte)  0x00, (byte) 0x00, (byte) 0x74}; 

The checksum should be (byte) 0x01,(byte) 0xf7, but how will be the method in Java to accomplish this? This array of bytes above is the example header of SECS-1 protocol. I use the for loop to sum all the bytes in integer, but I'm getting the result of 0x77 only which is far from 0x01 0xf7.

int sum =0;
for(int b:bytes){
    sum ^= b;
}
System.out.println(sum);

my second solution was

for (int i = 0; i < bytes.length; i++) {
    CheckSum += bytes[i];
}
System.out.println(Integer.toHexString(CheckSum));

but I'm getting the value 0xfffffef7, much closer to the value that I expect.


Solution

  • Java bytes are signed. Just get the unsigned value by using (b & 0xff) and then sum up.

    int sum =0;
    for(int b:bytes){
        sum += (b & 0xff);
    }
    System.out.println(sum);
    

    Then convert sum to byte array like in here Java integer to byte array