📄 crc-ccitt -- 16-bit.htm
字号:
<TR>
<TD><TT>Conversion of the binary value above to hexadecimal by segmenting
the bits to nibbles:</TT>
<BR><TT>
binary nibbles 1001 0100 0111 1001</TT>
<BR><TT>
hexadecimal
9 4 7
9</TT></TD></TR></TBODY></TABLE>
<P>
<HR width="100%">
<H3><A name=source></A>Source Code for the C-language Implementations</H3>
<TABLE cols=1 width="100%" border=1>
<TBODY>
<TR>
<TD><TT>/*</TT> <BR><TT>demonstrates how the incorrect check value of
0x29B1 may be reported</TT> <BR><TT>for the test string "123456789" when
it should be 0xE5CC.</TT> <BR><TT>*/</TT>
<P><TT>#include <stdio.h></TT> <BR><TT>#include
<string.h></TT>
<P><TT>#define
poly
0x1021 /* crc-ccitt
mask */</TT>
<P><TT>/* global variables */</TT> <BR><TT>char text[1000];</TT>
<BR><TT>unsigned short good_crc;</TT> <BR><TT>unsigned short bad_crc;</TT>
<BR><TT>unsigned short text_length;</TT>
<P><TT>int main(void)</TT> <BR><TT>{</TT> <BR><TT> void
go();</TT> <BR><TT> void repeat_character(unsigned char,
unsigned short);</TT>
<P><TT> sprintf(text, "%s", "");</TT>
<BR><TT> go();</TT>
<P><TT> sprintf(text, "%s", "A");</TT>
<BR><TT> go();</TT>
<P><TT> sprintf(text, "%s", "123456789");</TT>
<BR><TT> go();</TT>
<P><TT> repeat_character(65, 256);</TT>
<BR><TT> go();</TT>
<P><TT> return 0;</TT> <BR><TT>}</TT>
<P><TT>void go(void)</TT> <BR><TT>{</TT> <BR><TT> void
update_good_crc(unsigned short);</TT> <BR><TT> void
augment_message_for_good_crc();</TT> <BR><TT> void
update_bad_crc(unsigned short);</TT>
<P><TT> unsigned short ch, i;</TT>
<P><TT> good_crc = 0xffff;</TT>
<BR><TT> bad_crc = 0xffff;</TT>
<BR><TT> i = 0;</TT> <BR><TT>
text_length= 0;</TT> <BR><TT>
while((ch=text[i])!=0)</TT> <BR><TT> {</TT>
<BR><TT>
update_good_crc(ch);</TT>
<BR><TT>
update_bad_crc(ch);</TT>
<BR><TT> i++;</TT>
<BR><TT> text_length++;</TT>
<BR><TT> }</TT> <BR><TT>
augment_message_for_good_crc();</TT> <BR><TT>
printf(</TT> <BR><TT> "\nGood_CRC = %04X, Bad_CRC
= %04X, Length = %u, Text = \"%s\"",</TT>
<BR><TT>
good_crc,
bad_crc,
text_length, text</TT> <BR><TT> );</TT>
<BR><TT>}</TT>
<P><TT>void repeat_character(unsigned char ch, unsigned short n)</TT>
<BR><TT>{</TT> <BR><TT> unsigned short i;</TT>
<BR><TT> for (i=0; i<n; i++)</TT>
<BR><TT> {</TT>
<BR><TT> text[i] = ch;</TT>
<BR><TT> }</TT> <BR><TT> text[n] =
0;</TT> <BR><TT>}</TT>
<P><TT>void update_good_crc(unsigned short ch)</TT> <BR><TT>{</TT>
<BR><TT> unsigned short i, v, xor_flag;</TT>
<P><TT> /*</TT> <BR><TT> Align test
bit with leftmost bit of the message byte.</TT> <BR><TT>
*/</TT> <BR><TT> v = 0x80;</TT>
<P><TT> for (i=0; i<8; i++)</TT>
<BR><TT> {</TT>
<BR><TT> if (good_crc &
0x8000)</TT> <BR><TT> {</TT>
<BR><TT>
xor_flag= 1;</TT> <BR><TT>
}</TT> <BR><TT> else</TT>
<BR><TT> {</TT>
<BR><TT>
xor_flag= 0;</TT> <BR><TT>
}</TT> <BR><TT> good_crc =
good_crc << 1;</TT>
<P><TT> if (ch & v)</TT>
<BR><TT> {</TT>
<BR><TT>
/*</TT>
<BR><TT>
Append next bit of message to end of CRC if it is not zero.</TT>
<BR><TT>
The zero bit placed there by the shift above need not be</TT>
<BR><TT>
changed if the next bit of the message is zero.</TT>
<BR><TT>
*/</TT>
<BR><TT>
good_crc= good_crc + 1;</TT>
<BR><TT> }</TT>
<P><TT> if (xor_flag)</TT>
<BR><TT> {</TT>
<BR><TT>
good_crc = good_crc ^ poly;</TT>
<BR><TT> }</TT>
<P><TT> /*</TT>
<BR><TT> Align test bit with
next bit of the message byte.</TT>
<BR><TT> */</TT>
<BR><TT> v = v >> 1;</TT>
<BR><TT> }</TT> <BR><TT>}</TT>
<P><TT>void augment_message_for_good_crc()</TT> <BR><TT>{</TT>
<BR><TT> unsigned short i, xor_flag;</TT>
<P><TT> for (i=0; i<16; i++)</TT>
<BR><TT> {</TT>
<BR><TT> if (good_crc &
0x8000)</TT> <BR><TT> {</TT>
<BR><TT>
xor_flag= 1;</TT> <BR><TT>
}</TT> <BR><TT> else</TT>
<BR><TT> {</TT>
<BR><TT>
xor_flag= 0;</TT> <BR><TT>
}</TT> <BR><TT> good_crc =
good_crc << 1;</TT>
<P><TT> if (xor_flag)</TT>
<BR><TT> {</TT>
<BR><TT>
good_crc = good_crc ^ poly;</TT>
<BR><TT> }</TT>
<BR><TT> }</TT> <BR><TT>}</TT>
<P><TT>void update_bad_crc(unsigned short ch)</TT> <BR><TT>{</TT>
<BR><TT> /* based on code found at</TT>
<BR><TT>
http://www.programmingparadise.com/utility/crc.html</TT>
<BR><TT> */</TT>
<P><TT> unsigned short i, xor_flag;</TT>
<P><TT> /*</TT> <BR><TT> Why are they
shifting this byte left by 8 bits??</TT> <BR><TT> How do
the low bits of the poly ever see it?</TT> <BR><TT>
*/</TT> <BR><TT> ch<<=8;</TT>
<P><TT> for(i=0; i<8; i++)</TT>
<BR><TT> {</TT>
<BR><TT> if ((bad_crc ^ ch)
& 0x8000)</TT> <BR><TT>
{</TT>
<BR><TT>
xor_flag = 1;</TT> <BR><TT>
}</TT> <BR><TT> else</TT>
<BR><TT> {</TT>
<BR><TT>
xor_flag = 0;</TT> <BR><TT>
}</TT> <BR><TT> bad_crc =
bad_crc << 1;</TT>
<BR><TT> if (xor_flag)</TT>
<BR><TT> {</TT>
<BR><TT>
bad_crc = bad_crc ^ poly;</TT>
<BR><TT> }</TT>
<BR><TT> ch = ch <<
1;</TT> <BR><TT> }</TT>
<BR><TT>}</TT></P></TD></TR></TBODY></TABLE>
<P>
<HR width="100%">
<H3><A name=refs></A>References</H3>The following web page contains a javascript
calculator that is handy for what-if comparisions in calculating various CRCs by
slightly different methods and with any initial value -- very well done:
<UL>
<LI><A
href="http://rcswww.urz.tu-dresden.de/~sr21/crc.html">http://rcswww.urz.tu-dresden.de/~sr21/crc.html</A>
-- CRC calculation, by Sven Reifegerste. </LI></UL>
<P><BR>The following web pages were among those which were helpful in developing
the text and program in this document:
<UL>
<LI><A
href="http://www.ross.net/crc/crcpaper.html">http://www.ross.net/crc/crcpaper.html</A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -