Checksum calculation in android / java

I wrote a checksum calculation function in android / java. function as follows

void CalculateCheckSum( byte[] bytes ){ short CheckSum = 0, i = 0; for( i = 0; i < bytes.length; i++ ){ CheckSum = (short) ((short)CheckSum + (short)bytes[i]); } Log.i("Checksum", Integer.toHexString(CheckSum)); } 

input values ​​for calculating the checksum: 0xEF, 0x01, 0xEF, 0x01, 0x33, 0x0C, 0xB8, 0xE5, 0xFC, 0x34, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF. I manually calculated the value of the checksum and the result is 0xCE4. When using the above function, I get the answer as 0xFFFFFFE4. Is there any mistake in my calculations, if so, please correct me.

thanks

+6
source share
6 answers

byte according to Java Docs :

The byte value is between 2^(-7) and (2^7)-1 (-128 to 127).

But your 0xEF value (in decimal 239) has already reached the byte limit. What makes the amount give the wrong numbers.

+1
source

Use a debugger and debug your code. In general, if you continue to add files, even if you use int or long , at some point it should overflow, so you will get unexpected results. It is best to use a standard checksum algorithm or one of the classes already available, such as CRC32 or Adler31 . As for your code, you seem to treat the result as an integer, so why apply to short in the first place?

Java does all arithmetic using int , so your byte converted to int , and those that don't fit in bytes will look like int 's: ffffffef (-17). Naturally, you only need the actual byte value, so you need to zero out everything else using (0xff & b) . So your loop will look something like this:

  int checkSum = 0; for(byte b : bytes){ checkSum += (0xff & b); } 
+2
source

The problem is (short) different from bytes[i] . He expands the sign. You must change (short)bytes[i] to (bytes[i] & 0xff) . This will give you the correct answer.

This has nothing to do with byte overflow, contrary to most other answers. You also do not need to change the type of the array.

+2
source

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.

0
source

finally i got ...

fixed code

 void CalculateCheckSum( byte[] bytes ){ short CheckSum = 0, i = 0; for( i = 0; i < bytes.length; i++){ CheckSum += (short)(bytes[i] & 0xFF); } Log.i("Checksum", Integer.toHexString(CheckSum)); } 

thanks aprian and others

0
source

You should use integers as input if you convert them using:

  String CalculateCheckSum( Integer[] bytes ){ Integer CheckSum = 0, i = 0; for( i = 0; i < bytes.length; i++ ){ CheckSum += bytes[i]; } return Integer.toHexString(CheckSum); } 

this will return the expected 0xCE4 hope that this fixes your problem.

-1
source

Source: https://habr.com/ru/post/925092/


All Articles