How to use the crc16ccitt function from crc

Find comprehensive JavaScript crc.crc16ccitt code examples handpicked from public code repositorys.

crc.crc16ccitt is a function that calculates the cyclic redundancy check (CRC) for a given input data using the CCITT polynomial.

46
47
48
49
50
51
52
53
54
55
    payload.push(genEMV('61', cep));
}
payload.push(genEMV('62', genEMV('05', transactionId)));
payload.push('6304');
const stringPayload = payload.join('');
const crcResult = crc_1.crc16ccitt(stringPayload).toString(16).toUpperCase().padStart(4, '0');
const payloadPIX = `${stringPayload}${crcResult}`;
return {
    payload: () => payloadPIX,
    base64: (options) => qrcode_1.default.toDataURL(payloadPIX, options),
fork icon25
star icon157
watch icon0

60
61
62
63
64
65
66
67
68
69
    // rewind
    await fh.seek(0);
    var size = await fh.size();
    
    var buffer = await fh.read(size-2);
    var digest = crc.crc16ccitt(buffer); // .toString(16);
    
    return digest;
});

fork icon5
star icon14
watch icon0

How does crc.crc16ccitt work?

crc.crc16ccitt is a function that calculates the 16-bit CCITT CRC (cyclic redundancy check) checksum of a given buffer using the CCITT-16 polynomial algorithm. The function processes the input buffer in 8-bit chunks and returns the final calculated checksum.

60
61
62
63
64
65
66
67
68
69

  return gattBuffer;
}

function buildCrcBuffer(gst) {
  var crcValue  = crc.crc16ccitt(gst);
  var crcBuffer = new Buffer(2);
  crcBuffer.writeUInt16LE(crcValue, 0);

  return crcBuffer;
fork icon0
star icon9
watch icon2

1
2
3
4
5
6
7
8
9
10
11


function calculateCRC(data) {
  // Convert data to buffer
  const buffer = Buffer.from(data, 'utf8');
  // Calculate the CRC
  const result = crc.crc16ccitt(buffer, 0xffff);
  // Convert the CRC to hex string
  return result.toString(16).padStart(4, '0');
}

fork icon0
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
const crc = require("crc");

const buffer = Buffer.from("Hello World!", "utf8");
const result = crc.crc16ccitt(buffer);

console.log(result); // Output: 29428

In this example, we first import the crc module and then create a new buffer from a string. We then pass this buffer to the crc.crc16ccitt function, which calculates the CRC-16-CCITT checksum for the buffer's contents. Finally, we print the resulting checksum to the console.

77
78
79
80
81
82
83
84
85
86
87
88
89


}


// function verifyxml(msg, verify, param) {


// 	var _crc = crc.crc16ccitt(param.datetime + pwd + param.applid).toString(16);
// 	_crc = cfn.addZero(_crc, 4);
// 	var _verify = md5(_crc)
	
// 	return (_verify == verify);
fork icon0
star icon1
watch icon0

+ 13 other calls in file

96
97
98
99
100
101
102
103
104
105
}
else if (this.hbuf.length > 2) {
    var crcc = undefined;
    for (var _a = 0, _b = this.hbuf.slice(1, this.hbuf.length - 2); _a < _b.length; _a++) {
        var b = _b[_a];
        crcc = crc.crc16ccitt([b], crcc);
    }
    var cc = ((this.hbuf[this.hbuf.length - 1] << 8) | (this.hbuf[this.hbuf.length - 2] & 0xff));
    if (crcc == cc || this.had_escape) {
        this.emit('frame', this.hbuf.slice(1, this.hbuf.length - 2));
fork icon0
star icon0
watch icon1

133
134
135
136
137
138
139
140
141
142
  ...messageLen, // Tamanho da mensagem
  ...messageBytes, // A mensagem
  ...arrComandos,
  ETX,
];
const crcM = crc.crc16ccitt(bufferMsg);
if (crcM < 256) {
  bufferMsg.push(0);
  bufferMsg.push(crcM);
} else {
fork icon0
star icon0
watch icon0

94
95
96
97
98
99
100
101
102
103
//    nFrames, // Quantidade de frames total
//    ...messageLen, // Tamanho da mensagem
//    ...messageBytes, // A mensagem
//    ETX,
//  ];
//  const crcM = crc.crc16ccitt(bufferMsg);
//  // const crcM = calculaCrc(bufferMsg);
//  if (crcM < 256) {
//    bufferMsg.push(0);
//    bufferMsg.push(crcM);
fork icon0
star icon0
watch icon0