Files
step-free-api/test.js
2024-03-30 04:41:23 +08:00

18 lines
635 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function decimalToPaddedHex(decimal, size) {
// 创建一个新的Buffer大小为size个字节
const buffer = Buffer.alloc(size);
// 将十进制值写入Buffer从最后一个字节开始写即最低有效字节小端序
buffer.writeUInt32BE(decimal, size - 4); // 假设size至少为4
// 如果需要可以返回Buffer的十六进制表示
return buffer.toString('hex');
}
// 使用示例
const decimalValue = 10000000;
const size = 5; // 我们需要一个5字节的Buffer来存储这个值
const hexString = decimalToPaddedHex(decimalValue, size);
console.log(hexString); // 输出: 00 00 00 00 3c