node.js 小端十六進制的十進制互轉以及十六進制大小端轉換


十進制 轉 小端十六進制

function decToHex(num) {
    return (num + 2 ** 32).toString(16).match(/\B../g).reverse().join ``;
}

console.log(decToHex(300));
console.log(decToHex(30));
console.log(decToHex(230));

輸出:
2c010000
1e000000
e6000000

小端十六進制 轉 十進制

function hexToDec(num) {
    return Buffer.from(num, 'hex').readInt32LE()
}

console.log(hexToDec('2c010000'));
console.log(hexToDec('1e000000'));
console.log(hexToDec('e6000000'));

輸出:
300
30
230

十六進制大小端轉換

function endianConv(num) {
    return num.toString('hex').match(/.{2}/g).reverse().join("")
}

console.log(endianConv('2c010000'));
console.log(endianConv('1e000000'));
console.log(endianConv('e6000000'));

console.log(endianConv('0000012c'));
console.log(endianConv('0000001e'));
console.log(endianConv('000000e6'));

輸出:
0000012c
0000001e
000000e6
2c010000
1e000000
e6000000


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM