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,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to Programming forum
     
  3. Deepkamal singh

    Deepkamal singh New Member

    Joined:
    Aug 7, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    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
  4. Deepkamal singh

    Deepkamal singh New Member

    Joined:
    Aug 7, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Port of this function in Javascript

    PHP:
    this.getCheckSum = function (value) {
            var 
    buf value.split('');
            
    //console.log(buf);
            
    var result = new Array();
            for (var 
    0buf.length+= 4) {
              
    result[0] ^= parseInt(buf[i].charCodeAt(0));
              
    result[1] ^= parseInt(buf[i].charCodeAt(0));
              
    result[2] ^= parseInt(buf[i].charCodeAt(0));
              
    result[3] ^= parseInt(buf[i].charCodeAt(0));
            }

            
    result[0] = ~result[0];
            
    result[1] = ~result[1];
            
    result[2] = ~result[2];
            
    result[3] = ~result[3];
            var 
    retStr "";
            for (var 
    7>= 0i--) {
              
    result = ((2) === 1) ?
                (
    result[Math.floor(2)] & 0x0F) + 48 //Lower octet
                
    ((result[Math.floor(2)] >> 4) & 0x0F) + 48//Higher Octet
              //result=result + 'A' - '0' - 10
              
    result = (result 57) ? result result;
              
    retStr =+ String.fromCharCode(result) + retStr;
            }

            return 
    retStr;
          };
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice