js crc16检验

crc16(data) {
    let crcValue = 0xFFFF;
    for (let i = 0; i < data.length; i++) {
        crcValue ^= data[i] & 0xFFFF
        for (let j = 0; j < 8; j++) {
            if (crcValue & 0x0001) {
                crcValue >>= 1
                crcValue ^= 0xA001
            } else {
                crcValue >>= 1
            }
        }
    }

    crcValue = crcValue.toString(16)
    let crcArr = new Array(2)
    crcArr[0] = crcValue.substring(2, 4)
    crcArr[1] = crcValue.substring(0, 2)
    return crcArr
}

var res=crc16(Buffer.from("010101", "hex"));
console.log(res)

 

//要引入buffer
npm install buffer

import {Buffer  } from 'buffer'

  

posted @ 2022-08-18 10:39  evemen  阅读(1089)  评论(0编辑  收藏  举报