Answers for "crc calculator"

1

crc calculator

// CRC-CCITT (XModem)

const crc = function(str) {
    var crc = 0;
    for (var c = 0; c < str.length; c++) {
        crc ^= str.charCodeAt(c) << 8;
        for (var i = 0; i < 8; i++) {
            if (crc & 0x8000) crc = (crc << 1) ^ 0x1021;
            else crc = crc << 1;
        }
    }
    return crc & 0xFFFF;
}
    
const _hexStringToString = function(inputstr) {
    var hex = inputstr.toString(); //force conversion
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}


// Use 
console.log("0x" + crc(_hexStringToString("000100")).toString(16).toUpperCase())
// Expect 0x3331 (Input is array [ 0x00, 0x01, 0x00 ])


// References
// https://www.tahapaksu.com/crc/
// https://crccalc.com/
// https://reveng.sourceforge.io/crc-catalogue/16.htm
// https://www.acooke.org/cute/16bitCRCAl0.html
// https://barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
Posted by: Guest on July-13-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language