I am working on something and I am required to calculate checksum with every data sent. This checksum is the Lower 2 bytes of the 4 byte sum of all the bytes
Assume I have the data as:
Bytebuffer buffer = Bytebuffer.allocate(128); // assume i have filled buffer with some data
Below is how I am find the sum of all bytes and then getting the lower of 2 bytes.
byte array = buffer.array();
int checksum = 0;
for(byte b : array){
checksum += (int)b
}
checksum = (checksum & 0xff00ff) + ((checksum >> 0x08) & 0xff00ff);
short result = (short) (checksum + (checksum >> 0x10));
However, the application listening for this data tells me the checksum is wrong. What could be wrong in my logic of thinking? Thanks in Advance