3
我有一个4字节的数组。 32位无符号小端。将32位无符号小尾数转换为javascript中的整数
[ 123, 1, 0, 0]
我需要帮助将其转换为整数。我没有运气以下尝试:
let arr = [ 123, 1, 0, 0 ];
let uint = new Uint32Array(arr);
console.log('INT :', uint);
我有一个4字节的数组。 32位无符号小端。将32位无符号小尾数转换为javascript中的整数
[ 123, 1, 0, 0]
我需要帮助将其转换为整数。我没有运气以下尝试:
let arr = [ 123, 1, 0, 0 ];
let uint = new Uint32Array(arr);
console.log('INT :', uint);
有两种方式:
如果您知道您的浏览器也是小端(几乎总是如此,这些天),那么你可以做这样的:
const bytes = new Uint8Array([123, 1, 0, 0]);
const uint = new Uint32Array(bytes.buffer)[0];
console.log(uint);
如果您认为您的浏览器可能会在大端运行环境,你需要做的正确尾数转换,那么你这样做:
const bytes = new Uint8Array([123, 1, 0, 0]);
const dv = new DataView(bytes.buffer);
const uint = dv.getUint32(0, /* little endian data */ true);
console.log(uint);
8个字节?其他4个在哪里? – dandavis