Question

Recently I migrate an applicattion on tomcat5 with 32bit java to a tomcat6 with 64bits java.

In the application have a text CRC calculation, which returns different values ​​in each of the servers. For example: "Hipoplasia del seno frontal.Resto de senos faciales con desarrollo y neumatización habituales.No se observan lesiones óseas.Atentamente, " returns: 439231721 on 32 bit jvm and returns: 2756208468 on 64 bit jvm

Also tried it on a 32 bit jvm tomcat6 and returns 439231721

Here is the code of the crc calculation:

public static long doChecksum(String text) {

    try{

    //Convert string to bytes
    byte bytes[] = text.getBytes();
    Checksum checksum = new CRC32();
     // void update(bytes[] b, int start, int length)
    checksum.update(bytes,0,bytes.length);

    long lngChecksum = checksum.getValue();
            System.out.println(text + " : " + lngChecksum);
            return lngChecksum;
      } catch (Exception e) {
        return -1;
      }
}

Thanks!

Was it helpful?

Solution

Note that String.getBytes() translates characters to bytes using the default character encoding of the system. If the systems have different default character encodings, you get different byte arrays, leading to different checksums.

Specify the character encoding that you want to use to get consistent results:

byte[] bytes = text.getBytes("UTF-8");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top