1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

CRC/Checksum Code

Discussion in 'Programming' started by ASRRAJ, Jun 13, 2008.

  1. ASRRAJ

    ASRRAJ New Member

    Hi All,

    I m new to this group.
    I m developing some message handler for that i need to verify the Messages with some checksum bit.
    Can anybody help me out how to encode the messages with some checksum and vice versa.

    Regards,
    ASR
     
  2. shabbir

    shabbir Administrator Staff Member

    Moved to Programming forum
     
  3. Deepkamal singh

    Deepkamal singh New Member

    Code:
    public static String getCheckSum(String value) {
            int len = value.length();
            char[] buf;
            buf = value.toCharArray();
    
            int result[] = new int[28];
            for (int i = 0; i < len; i += 4) {
                result[0] ^= buf[0 + i];
                result[1] ^= buf[1 + i];
                result[2] ^= buf[2 + i];
                result[3] ^= buf[3 + i];
            }
    
            result[0] = ~result[0];
            result[1] = ~result[1];
            result[2] = ~result[2];
            result[3] = ~result[3];
    
            for (int i = 7; i >= 0; i--) {
                if (i % 2 == 1) // LOW
                {
                    result[i] = (result[i / 2] & 0x0F) + '0';
                    if (result[i] > '9') {
                        result[i] = result[i] + 'A' - '0' - 10;
                    }
                } else // HIGH
                {
                    result[i] = ((result[i / 2] >> 4) & 0x0F) + '0';
                    if (result[i] > '9') {
                        result[i] = result[i] + 'A' - '0' - 10;
                    }
                }
            }
    
            return "" + (char) result[0] + (char) result[1] + (char) result[2] + (char) result[3] + (char) result[4] + (char) result[5] + (char) result[6] + (char) result[7];
        }
     
    Last edited by a moderator: Aug 7, 2009

Share This Page