lizijie
2021-09-22 b8ad9afd4a2d4bcca3edb9d71cf4a53b480fa43f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function crc16_checkout2(puchMsg) {
    let CRC = 0xFFFF;
    let num = 0xA001;
    let inum = 0;
    var bytes = [];
    //let sb = puchMsg.getBytes();
    for (var i = 0; i < puchMsg.length; ++i) {
        bytes.push(puchMsg.charCodeAt(i));
    }
    let sb = puchMsg;
    for(let j = 0; j < sb.length; j ++) {
        inum = sb[j];
        CRC = (CRC >> 8) & 0x00FF;
        CRC ^= inum;
        for(let k = 0; k < 8; k++) {
            let flag = CRC % 2;
            CRC = CRC >> 1;
 
            if(flag == 1) {
                CRC = CRC ^ num;
            }
        }
    }
    return CRC;
}
 
// let b = 'QN=20160801085857223;ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&&';
// let a = crc16_checkout(b, b.length);
// console.log(a.toString(16).toUpperCase());
 
module.exports = crc16_checkout2;