As aprian mentioned above, while a byte has 8 bits for your hexadecimal values, it can only store values ββfrom -128 to 127. Therefore, a quick and easy solution would be to use the following larger primitive, short in this case.
short shorts[] = {0xEF, 0x01, 0xEF, 0x01, 0x33, 0x0C, 0xB8, 0xE5, 0xFC, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; int checkSum = 0; for( short s : shorts){ checkSum = checkSum + s; } System.out.println("Checksum: " + Integer.toHexString(checkSum));
This gives me the result:
Checksum: ce4
Of course, this means that you may have to convert your byte array in advance.
source share