Results 1 to 4 of 4
  1. #1
    Shrinath is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default CRC16 calculation through Java ?

    I am trying to calculate CRC CCITT checksum for given Hex string?

    This is how I tried
    Java Code:
    public class CRC16CCITT { 
    
        public static void main(String[] args) { 
            int crc = 0xFFFF;          // initial value
            int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 
    
        
            String str = "0009";
        
            byte []by = str.getBytes();
            for (byte b : by) {
                for (int i = 0; i < 8; i++) {
                    boolean bit = ((b   >> (7-i) & 1) == 1);
                    boolean c15 = ((crc >> 15    & 1) == 1);
                    crc <<= 1;
                    if (c15 ^ bit) crc ^= polynomial;
                 }
            }
    
            crc &= 0xffff;
            System.out.println(crc);
            System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
        }
    
    }
    -----------------

    expected output = 1d06
    actual output = cf63



    I have tried with many ways but I am not able to make it work.

    Can anybody help me in this?

    Thanks,
    Shrinath
    Last edited by Norm; 09-07-2011 at 01:41 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: CRC16 calculation through Java ?

    Can you show the algorithm, given the input how you would manually create the output?
    What are the intermediate values for that should be created?

  3. #3
    Shrinath is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: CRC16 calculation through Java ?

    Hi Norm,

    I have algorithm which is in C,
    Java Code:
    /** @fn void CRC_calcCrc8(u16 *crcReg, u16 poly, u16 u8Data)
    * @ Standard CRC calculation on an 8-bit piece of data. To make it
    * CCITT-16, use poly=0x1021 and an initial crcReg=0xFFFF.
    *
    * Note: This function allows one to call it repeatedly to continue
    * calculating a CRC. Thus, the first time it's called, it
    * should have an initial crcReg of 0xFFFF, after which it
    * can be called with its own result.
    *
    * @param *crcRegPointer to current CRC register.
    * @param poly Polynomial to apply.
    * @param u8Datau8 data to perform CRC on.
    * @return None.
    */
    void CRC_calcCrc8(u16 *crcReg, u16 poly, u16 u8Data)
    {
    u16 i;
    u16 xorFlag;
    u16 bit;
    u16 dcdBitMask = 0x80;
    for(i=0; i<8; i++)
    {
    // Get the carry bit. This determines if the polynomial should be
    // xor'd with the CRC register.
    xorFlag = *crcReg & 0x8000;
    // Shift the bits over by one.
    *crcReg <<= 1;
    // Shift in the next bit in the data byte
    bit = ((u8Data & dcdBitMask) == dcdBitMask);
    *crcReg |= bit;
    // XOR the polynomial
    if(xorFlag)
    {
    *crcReg = *crcReg ^ poly;
    }
    // Shift over the dcd mask
    dcdBitMask >>= 1;
    }
    }
    -------------------

    few i/ps and o/ps are
    (I/p is hex string)

    i/p - "0003" ==> o/p="1D0C"
    i/p - "0009" ==> o/p="1D06"

    Thanks for your reply, :)

    shrinath
    Last edited by Norm; 09-07-2011 at 01:57 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: CRC16 calculation through Java ?

    How will another program in another language help show the intermediate results that should be output by the algorithm so that they can be compared to what the intermediate results are from your program?

    I see that the c program has comments describing what it is trying to do.
    What happened to those comments when you wrote the java version?
    Last edited by Norm; 09-07-2011 at 02:07 PM.

Similar Threads

  1. Replies: 3
    Last Post: 04-24-2011, 11:06 AM
  2. Replies: 2
    Last Post: 04-23-2011, 03:27 PM
  3. Problem with calculation
    By ibrafiqb in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 05:02 PM
  4. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-24-2009, 11:44 PM
  5. RSSI calculation using Java Card STK....
    By vickytulla in forum Advanced Java
    Replies: 0
    Last Post: 07-14-2008, 08:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •