CRC/Checksum Code

ASRRAJ's Avatar, Join Date: Jun 2008
Light Poster
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Moved to Programming forum
Deepkamal singh's Avatar, Join Date: Aug 2009
Newbie 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 shabbir; 7Aug2009 at 18:57.. Reason: Code blocks
mayjune like this