1.异或(XOR)校验

// 异或(XOR)校验
function generateXORChecksum(data) {
    try {
        let checksum = 0;
        for(let i = 0; i < data.length; i += 2) {
            let item = data.substring(i, i+2);
            // 生成Unicode字符
            let charValue = String.fromCharCode('0x' + item);
            // 获取指定字符的十进制表示.
            let charCode = charValue.charCodeAt(0);
            checksum ^= charCode;
        }
        return checksum;
    } catch(err) {
        return "";
    }
}

2.累加校验

// 累加和校验(Checksum)
function generateAdditiveChecksum(data) {
    try {
        let checksum = 0;
        for(let i = 0; i < data.length; i += 2) {
            let item = data.substring(i, i+2);
            // console.log("item===", item);
            // 生成Unicode字符
            let charValue = String.fromCharCode('0x' + item);
            // console.log("charValue===", charValue);
            // 获取指定字符的十进制表示.
            let charCode = charValue.charCodeAt(0);
            // console.log("charCode===",charCode);
            checksum += charCode;
        }
        return checksum;
    } catch(err) {
        return "";
    }
}

3.16进制厘转10进制元

function centsStringToAmount(centsStr) {
   // 16转10
   let cents = parseInt(centsStr, 16);
   // 将厘转换为元
   let amount = cents / 100;
   // 返回结果
   return amount;
}
console.log("000001F4===",centsStringToAmount("000001F4"))
// 000001F4===5

4.10进制元转16进制厘

function formatAmountToFixedLength(amount) {
     // 将元转换为厘
     let cents = Math.round(amount * 100);
     // 10进制转16进制
     let centsHex = cents.toString(16).toUpperCase();
     // 确保长度为8,不足部分用0填充
     let centsStr = centsHex.toString().padStart(8, '0');
     // 返回结果
     return centsStr;  
}

console.log("5===",formatAmountToFixedLength(5))
// 5===000001F4

如有侵权请联系我删除。