⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc calculation.htm

📁 CRC16的源程序
💻 HTM
📖 第 1 页 / 共 2 页
字号:

  order=parseInt(document.crcform.order.value, 10);
  if (isNaN(order) == true || order<1 || order>64)
  {
    document.crcform.result.value = "CRC order must be between 1 and 64";
    return;
  }


  // convert crc polynom

  polynom = convertentry (document.crcform.polynom.value, order);
  if (polynom[0]<0)
  {
    document.crcform.result.value = "Invalid CRC polynom";
    return;
  }

  // check if polynom is valid (bit 0 must be set)
  
//  if (!(polynom[7]&1))
//  {
//    document.crcform.result.value = "CRC polynom LSB must be set";
//    return;
//  }

  

  // convert crc init value

  init = convertentry (document.crcform.init.value, order);
  if (init[0]<0)
  {
    document.crcform.result.value = "Invalid initial value";
    return;
  }


  // algorithm starts here (bit by bit type)
  // generate and byte mask

  counter = order;
  for (i=7; i>=0; i--)
  {
    if (counter>=8) mask[i] = 255;
    else mask[i]=(1<<counter)-1;
    counter-=8;
    if (counter<0) counter=0;
  }

  
  // compute new init value

  if (document.crcform.crcinittype[0].checked)		// nondirect -> direct
  {
    init[8] = 0;

    for (i=0; i<order; i++) {

      bit = init[7-((order-1)>>3)] & (1<<((order-1)&7));
      for (k=0; k<8; k++) {
        init[k] = ((init [k] << 1) | (init [k+1] >> 7)) & mask [k];
        if (bit) init[k]^= polynom[k];
      }
    }	
    document.crcform.crcinittype[0].checked=false;
    document.crcform.crcinittype[1].checked=true;
    document.crcform.result.value = "converted to direct initial value";
  }

  else
  {
    for (i=0; i<order; i++) {				// direct -> nondirect

      bit=init[7]&1;
      for (k=0; k<8; k++) if (bit) init[k]^= polynom[k];
      for (k=7; k; k--) init[k] = ((init [k] >> 1) | ((init[k-1]&1) << 7)) & mask [k];
      init[0]>>=1;
      if (bit) init[7-((order-1)>>3)] |= 1<<((order-1)&7);

    }	
    document.crcform.crcinittype[0].checked=true;
    document.crcform.crcinittype[1].checked=false;
    document.crcform.result.value = "converted to nondirect initial value";
  }


  
  // write result

  document.crcform.init.value = "";

  flag=0;
  for (i=0; i<8; i++)
  {
    actchar = init[i]>>4;
    if (flag || actchar)
    {
      document.crcform.init.value+= hexnum[actchar];
      flag=1;
    }

    actchar = init[i] & 15;
    if (flag || actchar || i==7)
    {
      document.crcform.init.value+= hexnum[actchar];
      flag=1;
    }
  }
}


function reflectByte(inbyte)
{
  // reflect one byte

  var outbyte=0;
  var i=0x01;
  var j;

  for (j=0x80; j; j>>=1) 
  {
    if (inbyte & i) outbyte|=j;
    i<<=1;
  }
  return (outbyte);
}


function reflect(crc, bitnum, startLSB)
{
  // reflect 'bitnum' bits starting at lowest bit = startLSB

  var i, j, iw, jw, bit;

  iw = 7;                        // LSByte
  jw = 1<<startLSB;              // LSBit, 0=bit0 (crc), 1=bit1 (poly)

  for (i=7-((bitnum-1)>>3); i<8; i++)
  {
    for (j=1<<((bitnum-1)&7); j && (!(i>iw || (i==iw && j<jw))); j>>=1)
    {
      bit = crc[iw] & jw;
      if (crc[i] & j) crc[iw] |= jw;
      else crc[iw] &= (0xff-jw);
      if (bit) crc[i] |= j;
      else crc[i] &= (0xff-j);

      jw = (jw<<1) & 0xff;
      if (!jw) {
        iw--;
        jw=1;
      }
    }
  }
  return(crc);
}


function convertentry (input, order)
{
  // convert from ascii to hexadecimal value (stored as byte sequence)

  var len;
  var actchar;
  var polynom = new Array (0,0,0,0,0,0,0,0);
  var brk = new Array (-1,0,0,0,0,0,0,0);

  // convert crc value into byte sequence

  len = input.length;
  for (i=0; i < len; i++)
  {
    actchar = parseInt(input.charAt(i), 16);
    if (isNaN(actchar) == true) return (brk);
    actchar&=15;

    for (j=0; j<7; j++) polynom[j] = ((polynom [j] << 4) | (polynom [j+1] >> 4)) & 255;
    polynom[7] = ((polynom[7] <<4) | actchar) & 255;
  }


  // compute and check crc order
 
  count = 64;
  for (i=0; i<8; i++)
  {
    for (j=0x80; j; j>>=1)
    {
      if (polynom[i] & j) break;
      count--;
    }
    if (polynom[i] & j) break;
  }

  if (count > order) return (brk);

  return(polynom);
}

//-->
</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY bgColor=#a2a2a2>
<FORM name=crcform><FONT face=Arial color=#e9e9e9 
size=4>&nbsp;&nbsp;&nbsp;<B>CRC parameters</B> <BR><FONT face=Arial 
size=1>&nbsp;<BR><!-- Tabelle -->
<TABLE cellSpacing=1 cellPadding=6 align=left border=0>
  <TBODY>
  <TR>
    <TD width=33></TD><!---------------------------------- Zeile CRC order  ---------------------->
    <TD width=200 bgColor=#214578><FONT face=Arial color=#d9a467 
      size=4>&nbsp;&nbsp;CRC order <FONT color=#996487>&nbsp;(1..64) 
      </FONT></FONT></TD>
    <TD width=120 bgColor=#315588><FONT face="courier new" size=4>&nbsp;<INPUT 
      maxLength=2 size=2 name=order> </FONT></TD>
    <TD></TD>
    <TD></TD></TR><!---------------------------------- Zeile CRC polynom  ---------------------->
  <TR>
    <TD></TD>
    <TD bgColor=#214578><FONT face=Arial color=#d9a467 size=4>&nbsp;&nbsp;CRC 
      polynom <FONT color=#996487>&nbsp;(hex) </FONT></FONT></TD>
    <TD bgColor=#315588><FONT face="courier new" size=4>&nbsp;<INPUT 
      maxLength=16 size=16 name=polynom> </FONT></TD>
    <TD><FONT face=Arial size=3><INPUT onclick=revpoly() type=button value=" reverse! "> </FONT></TD>
    <TD></TD></TR><!---------------------------------- Zeile CRC start ------------------------->
  <TR>
    <TD></TD>
    <TD bgColor=#214578><FONT face=Arial color=#d9a467 
      size=4>&nbsp;&nbsp;Initial value <FONT color=#996487>&nbsp;(hex) 
      </FONT></FONT></TD>
    <TD bgColor=#315588><FONT face="courier new" size=4>&nbsp;<INPUT 
      maxLength=16 size=16 name=init> </FONT></TD>
    <TD><FONT face=Arial size=3><INPUT onclick=chgtype() type=button value=" convert! "> </FONT></TD>
    <TD><FONT face=Arial color=#e9e9e9 size=4><INPUT type=radio 
      value=crcinitbitbybit name=crcinittype> nondirect &nbsp; <INPUT type=radio 
      value=crcinittable name=crcinittype> direct </FONT></TD><!---------------------------------- Zeile CRC final XOR ---------------------->
  <TR>
    <TD></TD>
    <TD bgColor=#214578><FONT face=Arial color=#d9a467 
      size=4>&nbsp;&nbsp;Final XOR value <FONT color=#996487>&nbsp;(hex) 
      </FONT></FONT></TD>
    <TD bgColor=#315588><FONT face="courier new" size=4>&nbsp;<INPUT 
      maxLength=16 size=16 name=xor> </FONT></TD>
    <TD></TD></TR></TBODY></TABLE><BR clear=all><!---------------------------------- Zeile mit buttons ------------------------><!-- Tabelle -->
<TABLE cellSpacing=1 cellPadding=8 align=left border=0>
  <TBODY>
  <TR>
    <TD width=21></TD>
    <TD><FONT face=Arial color=#e9e9e9 size=4><INPUT type=checkbox value=revin 
      name=reflect>&nbsp;reverse data bytes&nbsp;&nbsp;&nbsp;&nbsp; <INPUT 
      type=checkbox value=crcrevout name=reflect>&nbsp;reverse CRC result before 
      Final XOR </FONT></TD></TR>
  <TR>
    <TD></TD>
    <TD><FONT face=Arial size=3><INPUT onclick=clrpar() type=button value=" clear "> &nbsp; <INPUT onclick=setparcrcccitt() type=button value=" CRC-CCITT "> &nbsp; <INPUT onclick=setparcrc16() type=button value=" CRC-16 "> &nbsp; <INPUT onclick=setparcrc32() type=button value=" CRC-32 "> 
  </FONT></TD></TR></TBODY></TABLE><BR clear=all><FONT face=Arial color=#e9e9e9 
size=1>&nbsp;<BR><!------------------------ Zeile CRC char sequence -------------------------><!-- Tabelle --><FONT 
face=Arial color=#e9e9e9 size=4>&nbsp;&nbsp;&nbsp;<B>Data sequence</B> <BR>
<TABLE cellSpacing=1 cellPadding=7 align=left border=0>
  <TBODY>
  <TR>
    <TD width=21></TD>
    <TD><FONT face="courier new" size=4><INPUT maxLength=256 size=42 
      name=data> <FONT face=Arial color=#e9e9e9 size=3><INPUT onclick=clrdata() type=button value=" clear "> 
    </FONT></FONT></TD></TR></TBODY></TABLE><BR clear=all><FONT face=Arial 
size=1>&nbsp;<BR><!------------------------------- Zeile CRC result -------------------------><!-- Tabelle --><FONT 
face=Arial color=#e9e9e9 size=4>&nbsp;&nbsp;&nbsp;<B>Result</B> <BR>
<TABLE cellSpacing=1 cellPadding=7 align=left border=0>
  <TBODY>
  <TR>
    <TD width=21></TD>
    <TD><FONT face="courier new" size=4><INPUT onfocus=crcform.result.blur() 
      maxLength=256 size=42 name=result> <FONT face=Arial color=#e9e9e9 size=3><INPUT onclick=compute() type=button value=" compute! "> 
      </FONT></FONT></TD></TR></TBODY></TABLE><BR clear=all><FONT face=Arial 
size=1>&nbsp;<BR><FONT face=Arial color=#e9e9e9 size=4><BR><U>Version 
updates:</U><BR>4th of February 2003: outcommented the "LSB=1 test" in 
JAVASCRIPT and C code<BR>18th of January 2003: included crc masking after 
converting non-direct to direct initial crc (c-code only, javascript-code is and 
was correct)<BR>17th of January 2003: included comment concerning standard 
parameter set values (like CRC-CCITT), see below; included new links to crc 
pages<BR>13th of January 2003: in crctester.c: most of the int's replaced by 
unsigned longs to avoid compilation errors (especially on 16 bit 
machines)<BR><BR>This CRC calculator shall support hardware and software 
designers to check their specific CRC routine. Most of the theory for the 
JAVASCRIPT and the C code below is taken from the well-known PAINLESS GUIDE TO 
CRC ERROR DETECTION ALGORITHMS article written by Ross N. Williams.<BR><BR>The 
calculator has the following features: <BR>
<UL type=disc>
  <LI>CRC polynoms with orders of 1...64 (counted without the leading '1' bit). 
  <LI>reversed CRC polynoms can be easily determined.<BR>
  <LI>initial and final XOR values can be set. 
  <LI>initial CRC values of algorithms with (click 'nondirect') or without 
  (click 'direct') augmented zero bits can be converted from one into another. 
  <LI>both normal and reflected CRC (refin, refout). 
  <LI>common CRC parameter sets are given by buttons below the parameter 
  table.<BR>(please verify values, there are e. g. different init values for 
  calculating the CRC-CCITT, depending on if the algorithm is direct or 
  indirect. To assign an initial value to the direct or nondirect algorithm, you 
  can click the appropriate radio button on the right side.)<BR>
  <LI>data sequence can be either NULL or a sequence of characters and 
  hexadecimal values between %00...%FF, so is '%31%32%33' the same as '123'. 
  </LI></UL><BR>Furthermore, here is the complete free <A 
href="http://rcswww.urz.tu-dresden.de/~sr21/crctester.c">C CODE</A> of a simple 
CRC test programm that offer evaluation of different CRC algorithms (bit-by-bit 
and table-driven, each with and without augmented zero bytes). For the 
JAVASCRIPT code see the HTML source ;-) I checked the resulting CRC with those 
values computed by some simple CRC calculators that I found in the internet (see 
links) and the values given in the PAINLESS GUIDE TO CRC ERROR DETECTION 
ALGORITHMS article. Anyway, if there are any errors in computing the CRC, either 
in the above JAVASCRIPT calculator or in the C program, please contact me under 
<A href="mailto:zorci@gmx.de">zorci@gmx.de</A>. <BR><BR>Next is a list of CRC 
polynoms that I could find: <FONT face="courier new" color=#000000 size=4><PRE>   CCITT-32:   0x04C11DB7  =  x<SUP>32</SUP> + x<SUP>26</SUP> +  x<SUP>23</SUP> + x<SUP>22</SUP> + x<SUP>16</SUP> + x<SUP>12</SUP> +
                              x<SUP>11</SUP> + x<SUP>10</SUP> + x<SUP>8</SUP> + x<SUP>7</SUP> + x<SUP>5</SUP> + x<SUP>4</SUP> + x<SUP>2</SUP> + x + 1

   CRC-16:     0x8005      =  x<SUP>16</SUP> + x<SUP>15</SUP> + x<SUP>2</SUP> + 1

   CRC-CCITT:  0x1021      =  x<SUP>16</SUP> + x<SUP>12</SUP> + x<SUP>5</SUP> + 1

   CRC-XMODEM: 0x8408      =  x<SUP>16</SUP> + x<SUP>15</SUP> + x<SUP>10</SUP> + x<SUP>3</SUP>

   12bit-CRC:  0x80f       =  x<SUP>12</SUP> + x<SUP>11</SUP> + x<SUP>3</SUP> + x<SUP>2</SUP> + x + 1

   10bit-CRC:  0x233       =  x<SUP>10</SUP> + x<SUP>9</SUP>  + x<SUP>5</SUP>  + x<SUP>4</SUP>  + x  + 1

   8bit-CRC:   0x07        =  x<SUP>8</SUP>  + x<SUP>2</SUP>  + x + 1

</PRE><FONT face=Arial color=#e9e9e9 size=4>Finally here are some links to CRC 
and JAVASCRIPT relavant websites: <PRE>   <A href="http://www.cs.waikato.ac.nz/~312/crc.txt">www.cs.waikato.ac.nz/~312/crc.txt</A>(Painless guide)
   <A href="http://www.seanet.com/~ksbrown/kmath458.htm">www.seanet.com/~ksbrown/kmath458.htm</A>
   <A href="http://www.geocities.com/CapeCanaveral/Launchpad/3632/crcguide.htm">www.geocities.com/CapeCanaveral/Launchpad/3632/crcguide.htm</A>
   <A href="http://utopia.knoware.nl/users/eprebel/Communication/CRC/index.html">utopia.knoware.nl/users/eprebel/Communication/CRC/index.html</A>
   <A href="http://www.iti.fh-flensburg.de/lang/algorithmen/code/crc/crc.htm">www.iti.fh-flensburg.de/lang/algorithmen/code/crc/crc.htm</A>
   <A href="http://www.eagleairaust.com.au/code/crc16.htm">http://www.eagleairaust.com.au/code/crc16.htm</A>
   <A href="http://www.s-direktnet.de/homepages/neumann/Data/Michael/Algorithmen/Crc/Crc16.cpp">www.s-direktnet.de/homepages/neumann/Data/Michael/Algorithmen/Crc/Crc16.cpp</A>
   <A href="http://members.dencity.com/jas/fravia/crctut1.htm">members.dencity.com/jas/fravia/crctut1.htm</A>
   <A href="http://ftp.std.com/obi/Standards/FileTransfer/XMODEM-CRC.NOTE.1">http://ftp.std.com/obi/Standards/FileTransfer/XMODEM-CRC.NOTE.1</A>
   <A href="http://www.ross.net/crc/links.html">www.ross.net/crc/links.html</A>
   <A href="http://www.smbus.org/faq/crc8Applet.htm">www.smbus.org/faq/crc8Applet.htm</A>  (JAVA CRC-8 calculator)
   <A href="http://hyperarchive.lcs.mit.edu/HyperArchive/Archive/per/csmp/csmp-digest-v3-028.txt">hyperarchive.lcs.mit.edu/HyperArchive/Archive/per/csmp/csmp-digest-v3-028.txt</A>
   <A href="http://www.joegeluso.com/software/articles/ccitt.htm#results">CRC-CCITT discussion</A>
   <A href="http://home.att.net/~cbwatson/CRC_info.doc">more test case values</A>

   <A href="http://www.teamone.de/selfhtml/">http://www.teamone.de/selfhtml/</A>  (JAVASCRIPT)
   <A href="http://www.netzwelt.com/selfhtml/">http://www.netzwelt.com/selfhtml/</A>  (JAVASCRIPT)
   <A href="http://www.rabich.de/">http://www.rabich.de/</A>  (JAVASCRIPT)

</PRE><FONT face=Arial color=#e9e9e9 size=4>So have a nice time even in a 
reflected world :-)<BR>Sven Reifegerste<BR></FORM>
<SCRIPT language=JavaScript>
  resetform();
</SCRIPT>
<A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/files/file1.html">freenet.de/files/file1.html</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/files/">freenet.de/files/</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/files/file1.html">/files/file1.html</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/files">freenet.de/files</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/files/">/files/</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/files/file1.html">files/file1.html</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/">freenet.de/</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/files">/files</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/files/">files/</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/file1.html">/file1.html</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de">freenet.de</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/">/</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/files">files</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/file1.html">file1.html</A><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/file1.html">freenet.de/file1.html</A><BR><BR><A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de/%20file1.html">freenet.de/ 
file1.html</A><BR><BR><BR><BR><A 
href="http://rcswww.urz.tu-dresden.de/">TESTLINK!!!</A> <A 
href="http://rcswww.urz.tu-dresden.de/../index.html">TESTLINK!!!</A> <A 
href="http://rcswww.urz.tu-dresden.de/~sr21/abc.html">TESTLINK!!!</A> <A 
href="http://rcswww.urz.tu-dresden.de/~sr21/freenet.de">TESTLINK!!!</A> <A 
href="http://rcswww.urz.tu-dresden.de/~sr21/www.freenet.de">TESTLINK!!!</A> <A 
href="file://www.freenet.de/">TESTLINK!!!</A> <A 
href="file:///c:/www.freenet.de">TESTLINK!!!</A> <A 
href="http://c:/www.freenet.de">TESTLINK!!!</A> 
</FONT></FONT></FONT></FONT></FONT></FONT></FONT></FONT></FONT></FONT></FONT></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -