2016-07-26 55 views

回答

3

有兩種方式:

如果您知道您的瀏覽器也是小端(幾乎總是如此,這些天),那麼你可以做這樣的:

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);

相關問題