Does this seem correct? I m trying to port this checksum method from C to Java but the values returned from my Java function are too high. I suspect the way I m fudging the unsigned int is not correct?
char *Generate( char * buffer, long length ) {
static char tBuf[4];
long index;
unsigned int checksum;
for( index = 0L, checksum = 0; index < length; checksum += (unsigned int) buffer[index++] );
sprintf( tBuf, "%03d", (unsigned int) ( checksum % 256 ) );
return( tBuf );
}
My Java port:
public String generateCheckSum( String value ) {
char[] chars = value.toCharArray();
long checksum = 0L;
for( int i = 0; i < chars.length; i++ ) {
checksum += (checksum + (long) chars[ i ]) & 0xFF;
}
return String.valueOf( checksum % 256 );
}
Any insight is very much appreciated, thanks