How To Calculate CRC16 Using CRC-CCITT ( Kermit )

Hi, Expert

I need to calculate CRC16  Using CRC-CCITT ( Kermit ) using C# for our GPS Device.

Please send me the complete code in C#.
Asrar AzmiWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill BachPresident and Btrieve GuruCommented:
0
Asrar AzmiWeb DeveloperAuthor Commented:
Hi, Expert I checked the link and tested the code i think it has some bugs.

I am giving you the code for Calculating CRC16 , but it is in C Language which is i dont understand if you convert it into c# it will be good for me.

CRC calculation algorithm is shown below (C programming language).
/*----------------------------------------------------------------------------
* FUNCTION: CRC16
*---------------------------------------------------------------------------*/
unsigned short crc_16_rec (char *pucData, unsigned short ucLen) {
//--------------------------------------------------------------------
unsigned int i;
unsigned char ucBit, ucCarry;
//--------------------------------------------------------------------
unsigned short usPoly = 0x8408;//reversed 0x1021
unsigned short usCRC = 0;
//--------------------------------------------------------------------
for (i = 0; i < ucLen; i++) {
usCRC ^= pucData[i];
for (ucBit = 0; ucBit < 8; ucBit++) {
ucCarry = usCRC & 1;
usCRC >>= 1;
if (ucCarry) {
usCRC ^= usPoly;
}
}
}
//--------------------------------------------------------------------
return usCRC;
//--------------------------------------------------------------------
}

Open in new window

0
Bill BachPresident and Btrieve GuruCommented:
C and C# are very similar, especially when you are looking INSIDE of a function.  You onl;y need to deal with the fact that you don't have access to a "string" pointer, but instead you have an array of BYTE values.  Because of this, you should only need to change how you are accessing the source array.  Try this:
CRC calculation algorithm is shown below (C programming language).
/*----------------------------------------------------------------------------
* FUNCTION: CRC16
*---------------------------------------------------------------------------*/
unsigned short crc_16_rec (byte[] pucData) {
//--------------------------------------------------------------------
unsigned int i;
byte c;
unsigned char ucBit, ucCarry;
//--------------------------------------------------------------------
unsigned short usPoly = 0x8408;//reversed 0x1021
unsigned short usCRC = 0;
//--------------------------------------------------------------------
for (i = 0; i < val.Length; i++) {
c = val[i]
usCRC ^= c;
for (ucBit = 0; ucBit < 8; ucBit++) {
ucCarry = usCRC & 1;
usCRC >>= 1;
if (ucCarry) {
usCRC ^= usPoly;
}
}
}
//--------------------------------------------------------------------
return usCRC;
//--------------------------------------------------------------------
}

Open in new window

0
Asrar AzmiWeb DeveloperAuthor Commented:
Hi, Expert

The changes you made in the code, it has still bugs in it
can you please test the code and send it back to me becoz im not a expert programmer like you.

Thanks
0
Bill BachPresident and Btrieve GuruCommented:
Srry -- Copy/Paste error.  Change pucdata in the function declaration to "val" instead.  If you notice, all I did was change your C code, using an unsigned char array to a byte array.

I am teaching a class all week and won't be back to my office until next week.  If you still find "bugs", please be more descriptive -- Does it not compile?  Does it not produce the correct output?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.