1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

CRC/Checksum Code

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

  1. ASRRAJ

    ASRRAJ New Member

    Joined:
    Jun 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Moved to Programming forum
     
  3. Deepkamal singh

    Deepkamal singh New Member

    Joined:
    Aug 7, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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