- checkSum校验:
export function checkSum(str){
const byteArray = []
for (let i = 0; i < str.length; i+=2) {
byteArray[i / 2] = str.substring(i, i + 2)
}
const sum = byteArray.reduce((pre, cur) => {
return pre + parseInt(cur, 16)
}, 0)
return sum
}
- bcc校验
export function bccCheck(str) {
const hexStrs = str.split(' ')
let xorDecimal = hexStrs.reduce((pre, cur) => {
return pre ^ parseInt(cur, 16)
}, 0)
console.log('十进制的bcc校验码:', xorDecimal);
const bccHex = Dec2Hex(xorDecimal)
console.log('二进制的bcc校验码:', bccHex);
return bccHex
}
export function Dec2Hex(dec) {
return baseConvertion(dec, 10, 16)
}
function baseConvertion(num, from, to) {
return parseInt(num, from).toString(to)
}