bcc校验和chs校验

  1. 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
}
  1. 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)
}
posted @ 2025-04-14 16:49  躺尸的大笨鸟  阅读(64)  评论(0)    收藏  举报