Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Calculating Checksum Java

Options
  • 24-06-2003 9:37am
    #1
    Closed Accounts Posts: 536 ✭✭✭


    I'm sending a byte array to a C server and they are calculating the checksum using the following algorithm :
    uint32 CheckSum32(uint32* p_buf, uint nbytes)
    {
    uint i;
    uint32 sum = 0;

    for(i = 0; i < nbytes/4; i++){
    sum += p_buf;
    }

    return sum;
    }

    I need to calculate the same checksum on my Java side

    What I'm doing at the moment is :
    long checksum = 0;
    for(int c = 0; c < byte_buffer.length/4; c+=4)
    {
    checksum += convertHexArrayToLong(byte_buffer, 4, c);
    }

    The function convertHexArrayToLong takes 4 bytes ( 0x23 , 0x45, 0x51, 0x94) and converts it to a long in the format 0x23455194.

    I've tried using an int value as well but no joy.

    I'm thinking it's to do with the different range if values for a uint32 in C and an int or long in Java.

    Can someone look at the C code above and suggest what I could do in Java to calculate the same checksum please?


Comments

  • Registered Users Posts: 1,931 ✭✭✭Zab


    Well, I have a few observations anyway.

    You are correct about the range of a uint32 and a java int being an issue. However, you can get around this in java by using a long ( as you are ) and after the += calculation do a
    checksum &= 0xFFFFFFFF;
    

    This will snip off the bits that are over what a uint32 can hold.

    I'm unsure what you are doing with the nbytes in the C code. If this is actually the number of bytes ( with four bytes in each uint32 ) then the java code is only iterating through a quarter of what the C code is iterating.

    Also, the convertHexArrayToLong is a little confusing, as I don't see a hex array anywhere... I see four bytes that you want to turn into a long. You should also keep the upper bound in mind ( what happens if the array is not divisable by four ).

    Zab.


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by Zab
    Well, I have a few observations anyway.

    You are correct about the range of a uint32 and a java int being an issue. However, you can get around this in java by using a long ( as you are ) and after the += calculation do a

    checksum &= 0xFFFFFFFF;
    

    This will snip off the bits that are over what a uint32 can hold.
    thanks I'll give that a try

    I'm unsure what you are doing with the nbytes in the C code. If this is actually the number of bytes ( with four bytes in each uint32 ) then the java code is only iterating through a quarter of what the C code is iterating.
    To get the checksum you take 4 bytes at a time and add them together. Each 'element' in the uint32* p_buf is 4 bytes long.

    Also, the convertHexArrayToLong is a little confusing, as I don't see a hex array anywhere... I see four bytes that you want to turn into a long. You should also keep the upper bound in mind ( what happens if the array is not divisable by four ).

    Zab.

    sorry the parameters for the convertHexArrayToLong are :
    long convertHexArrayToLong( byte[] byte_array, int numBytes, int offset);

    I also pad the array out to be 4 byte aligned before I calculate the checksum.


  • Registered Users Posts: 1,931 ✭✭✭Zab


    Fair enough. However, you are still only iterating a quarter of the array in the java version. And there is no "hex array", just an array of bytes. I'm sure the method works, it's just the name that I have an issue with.


  • Closed Accounts Posts: 536 ✭✭✭flyz


    Originally posted by Zab
    Fair enough. However, you are still only iterating a quarter of the array in the java version. And there is no "hex array", just an array of bytes. I'm sure the method works, it's just the name that I have an issue with.

    Yes you were right, I think I did so many things to try and make it work that I forgot to change things back :)

    this is what I've finished with anyway.
    you have to specify mask as : 0xFFFFFFFL otherwise it won't work.
    long checksum = 0;
    long temp_checksum = 0;
    for(int c = 0; c < temp_array.length; c+=4)
    {
      temp_checksum = SesPages.convertHexArrayToLon(temp_array, 4, c);
         
     checksum = (checksum + temp_checksum)&0xFFFFFFFFL;
    }
    
    


  • Registered Users Posts: 1,931 ✭✭✭Zab


    I'm presuming it works? I remembered that it would need the L after I posted, but forgot to mention it in my second post, sorry.


  • Advertisement
  • Closed Accounts Posts: 358 ✭✭CH


    i found this tutorial usefull a few months ago: http://www.acte.no/teknisk/html/crcguide.htm
    int DATA_SIZE = 8;
    int CHK_SEED = 127;
    
    long l; 
    int chkSum; 
    
    while( run ) 
    { 
    	l = 0;				// reset 
    	chkSum = 0; 
    
    	readData();			// blocking call: read data frame
    	buffer.rewind();		// position = 0 
    
    	// calc chksum for frame
    	while( buffer.position() < ( DATA_SIZE - 1 ) ) 
    	{ 
    		l = ( l << 8 ) + ( buffer.get() & 0xFF ); 
    	} 
    	chkSum = ( int )( l % CHK_SEED );
    
    	// compare chkSum with last byte in frame
    	if( chkSum == buffer.get() )	// postition = 7
    	{ 
    		// chksums match
    	} 
    	else
    	{
    		// chksums dont match
    	}
    }
    


Advertisement