<html>
<title>CRC Prefix Calculator</title>
<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>
<h1>CRC Prefix Calculator</h1>
<p>Enter a string of hex bytes in the box and hit the <b>Find CRC init</b> 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.
</p>
<form name="crcform"> 
<textarea name="hexstring" rows="5" cols="80">30FFFFFFFFFFFF50CF5DD9E2C0B80065F5D1A483F0FBBC6F01001E6C043B517E90B286
</textarea>
<br>
<label>Initial CRC value (hex):</label>
<label id="initcrc">5FD6</label>
<br>
<input name="submit" type="button" value="Find CRC init" onclick="calculateInit()">
</form>
 
 
</body></html>