Results 1 to 4 of 4
Thread: CRC16 calculation through Java ?
 09072011, 12:36 PM #1Member
 Join Date
 Sep 2011
 Posts
 3
 Rep Power
 0
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 >> (7i) & 1) == 1); boolean c15 = ((crc >> 15 & 1) == 1); crc <<= 1; if (c15 ^ bit) crc ^= polynomial; } } crc &= 0xffff; System.out.println(crc); System.out.println("CRC16CCITT = " + 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,
ShrinathLast edited by Norm; 09072011 at 02:41 PM. Reason: added code tags
 09072011, 02:41 PM #2
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?
 09072011, 02:49 PM #3Member
 Join Date
 Sep 2011
 Posts
 3
 Rep Power
 0
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 8bit piece of data. To make it * CCITT16, 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, :)
shrinathLast edited by Norm; 09072011 at 02:57 PM. Reason: added code tags
 09072011, 02:57 PM #4
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; 09072011 at 03:07 PM.
Similar Threads

Hi i'm new to java....I need help to finish this calculation,can anyone help me.....
By double D in forum New To JavaReplies: 3Last Post: 04242011, 12:06 PM 
Can anyone help me to complete this java calculation????
By double D in forum New To JavaReplies: 2Last Post: 04232011, 04:27 PM 
Problem with calculation
By ibrafiqb in forum New To JavaReplies: 3Last Post: 11242010, 06:02 PM 
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02252009, 12:44 AM 
RSSI calculation using Java Card STK....
By vickytulla in forum Advanced JavaReplies: 0Last Post: 07142008, 09:56 AM
Bookmarks