Open main menu

Widget:CRCprefix

Revision as of 19:52, 21 May 2022 by Hash (talk | contribs) (Created page with "<html><link type="text/css" rel="stylesheet" id="dark-mode-custom-link"><link type="text/css" rel="stylesheet" id="dark-mode-general-link"><style lang="en" type="text/css" id=...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<html><link type="text/css" rel="stylesheet" id="dark-mode-custom-link"><link type="text/css" rel="stylesheet" id="dark-mode-general-link"><style lang="en" type="text/css" id="dark-mode-custom-style"></style><style lang="en" type="text/css" id="dark-mode-native-style"></style><head lang="en"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>CRC Prefix Calculator</title> <style type="text/css"> /*<![CDATA[*/ body {

   font-style:normal;
   font-family:Verdana,Arial, sans-serif;
   background-color:white;
   color:black;
   margin-left:5%;

} h1 {

   text-indent:1em;
   color:white;
   background-color:navy;

} h2 {

   text-indent:2em;
   color:white;
   background-color:navy;

} h3 {

   margin-left:1em;
   margin-right:1em;
   text-indent:2em;
   color:white;
   background-color:navy;

} table {

   border-style:solid;

} td {

   padding-left:20px;

} caption {

   font-style:oblique;

} /*]]>*/ </style> <script type="text/javascript"> //<![CDATA[ function calculateInit () {

   str = document.crcform.hexstring.value.toString();
   var prefix = crc.calculateInitFunc(str);
   document.getElementById("initcrc").textContent = 
                   prefix.toString(16).toUpperCase().padStart(4, "0");

}

var crc = (function() {

   applyCRC = function(crc, value) {
       crc ^= value << 8;
       crc &= 0xffff;
       for (bit = 0; bit < 8; bit++) {
           var olddata = crc & 0x8000;
           crc <<= 1;
           if (olddata) {
               crc ^= 0x1021;
           }
           crc &= 0xffff;
       }
       return crc;
   }
   calcCRC = function(pkt, crc) {
       return pkt.reduce(applyCRC, crc);
   }
   stringToBytes = function(s){
       return s.split(/([a-fA-F0-9][a-fA-F0-9])/g).filter(p => !!p).map(c => parseInt(c, 16));
   }
   bytesToString = function(b) {
       return b.map(c => c.toString(16).padStart(2,"0")).join("");
   }
   return {
       calculateInitFunc : function (str) {
           str = str.toUpperCase().replace(/\s/g,"");
           var bytes = stringToBytes(str);
           for (testvalue = 0; testvalue < 65536; testvalue+=1) {
               if (calcCRC(bytes,testvalue) === 0) {
                   return testvalue;
               }
           }
       }
   };

})(); //]]> </script> </head> <body>

CRC Prefix Calculator

Enter a string of hex bytes in the box and hit the Find CRC init button searches for the initial CRC value which would make the calculated CRC equal to zero. It uses the 0x1021 polynomial value. If it cannot find such a value, it will leave the value unchanged.

<form name="crcform"> <textarea name="hexstring" rows="5" cols="80">30FFFFFFFFFFFF50CF5DD9E2C0B80065F5D1A483F0FBBC6F01001E6C043B517E90B286 </textarea>
<label>Initial CRC value (hex):</label> <label id="initcrc">5FD6</label>
<input name="submit" type="button" value="Find CRC init" onclick="calculateInit()"> </form>


</body></html>