English 中文(简体)
C to Java port: calculating a 3 byte checksum
原标题:

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

最佳回答

C++

checksum += (unsigned int) buffer[index++]

Java

checksum += (checksum + (long) chars[ i ]) & 0xFF

There s an extra checksum in there

问题回答

A shorter version

public String generateCheckSum(String value) {        
    int checksum = 0;
    for(char ch : value.toCharArray()) checksum += ch;
    return "" + (checksum & 0xFF);
}

Note: checksum % 256 and checksum & 0xFF don t do the same thing. The first is signed and can return a negative result, the second will not.

Thank you for all of the feedback, it allowed me to get to my final solution. I m sure it could be tweaked further but it does produce the desired checksum values.

public Integer generateCheckSum(String value) throws UnsupportedEncodingException {

    byte[] data = value.getBytes("US-ASCII");        
    long checksum = 0L;

    for( byte b : data )  {
        checksum += b; 
    }

    checksum = checksum % 256;

    return new Long( checksum ).intValue();

}

Don t you think in your Java code

checksum += (checksum + (long) chars[ i ]) & 0xFF;

should be

checksum += ((long) chars[ i ]) & 0xFF;

?

Also in your C function, you are using incorrect format specifier for unsigned int. It should be %u instead of %d.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签