Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculating a CRC-16 checksum

1,957 views
Skip to first unread message

Tim

unread,
Nov 12, 1998, 3:00:00 AM11/12/98
to
What I need to do is write a function in C to
calculate the CRC-16 checksum of an array of bytes.
I wrote a simple function to do this from what I
learned about CRC from:
http://www.cubic.org/source/archive/coding/algorith/misc/crc.txt
but when I tested it with the CRC-16 Check value (The
checksum of "123456789") but no matter how much I
tweaked it, it never returned the correct checksum
(0xBB3D). Hex Workshop must use the exact same CRC-16
algorithm that I am trying to duplicate because it
gave 0xBB3D as the CRC-16 checksum for "123456789". If
someone out there has a C/C++ function to generate the
same CRC-16 checksum as Hex Workshop, could you PLEASE
respond to this posting with the code? Or if you can
find what is wrong with my function I would appreciate
that too.

Thank you
- Tim

// My attempt at generating a CRC-16 checksum
// reflectByte() and reflectWord() are functions
// I wrote and tested to reverse the order of the bits
// because the description of the CRC-16 algorithm
// from:
// http://www.cubic.org/source/archive/coding/algorith/misc/crc.txt
//
// Name : "CRC-16"
// Width : 16
// Poly : 8005
// Init : 0000
// RefIn : True
// RefOut : True
// XorOut : 0000
// Check : BB3D
// Says that each byte is reflected before being
// processed (RefIn) and the final register (checksum)
// value is reflected (RefOut).
//

WORD crc16(BYTE buf[], UINT nSize)
{
WORD reg = 0; // Checksum register.
WORD POLY = 0x8005, // Divisor Polynomial.
wPop; // Used to test wether to XOR the register
UINT n;
BYTE bit, curByte;


for(n = 0; n < nSize; n++)
{
// Reflect the byte
curByte = reflectByte(buf[n]);

// start with bit 7 and loop to bit 0
for(bit = 0x80; bit; bit = (bit / 2))
{
wPop = reg & 0x8000;
reg <<= 1;

if(curByte & bit)
reg |= 0x0001;

// if the bit popped off the register was set
if(wPop)
reg ^= POLY; // XOR in the polynomial
}
}

// reflect register value
reg = reflectWord(reg);

// XOR in the XOROut value
reg ^= 0x0000;

return reg;
}


Tim

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to

Scott G. Hall

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to Tim
Tim wrote:
> What I need to do is write a function in C to
> calculate the CRC-16 checksum of an array of bytes.
> If someone out there has a C/C++ function to generate the
> same CRC-16 checksum as Hex Workshop, could you PLEASE
> respond to this posting with the code?

Go to the Usenet archives (such as DejaNews) and pull up a copy of Ward
Christensen's XMODEM source, and look how it is done in there.
ftp://ftp.cs.pdx.edu/pub/zmodem/rzsz.zip

Then check the page: "The Great CRC Mystery" by Terry Ritter
http://www.io.com/~ritter/ARTS/CRCMYST.HTM

And "CRC: Resources On The Net
This page contains links to other CRC resources on the Internet."
http://www.ross.net/crc/links.html

And "Fil's FAQ-Link-In Corner: CRC algorithms"
http://www.repairfaq.org/filipg/LINK/F_crc_v35.html

I noticed another page (in another language) with an implementation:
http://www.robustdc.com/crc_vb.txt

And "CRC.DYL: A DYL for calculating common CRCs"
http://ourworld.compuserve.com/homepages/cade/crc.htm

(and this is just out of my bookmarks...)
--
Scott G. Hall
GTE Government Systems
North Carolina Systems Center
email: Scott...@GSC.GTE.Com

Scott G. Hall

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
0 new messages