0
我試圖在JavaScript中重現以下Python代碼。嘗試使用TypedArrays在JavaScript中複製Python的struct.unpack「> L」
import struct
val = struct.unpack(">L", "MACS")[0]
val
現在是1296122707
(如0x4d414353
相同)。與htk1
一樣嘗試使用1752460081
,與0x68746b31
相同。
我試圖讓這件事的JavaScript,所以我得學習過這個文檔上的Python傳來:classmethod int.from_bytes()
所以上面是一樣的:
int.from_bytes(b"MACS", "big")
不過我無法將其移植到JavaScript。你能幫我理解這一點嗎?或者如果它已經在那裏可用?
這裏是我的嘗試:
function unpackL(fourCharCode) {
var buf = new ArrayBuffer(8);
var view = new DataView(buf);
view.setUint8(0, String.charCodeAt(fourCharCode[0]), true);
view.setUint8(2, String.charCodeAt(fourCharCode[1]), true);
view.setUint8(4, String.charCodeAt(fourCharCode[2]), true);
view.setUint8(6, String.charCodeAt(fourCharCode[3]), true);
return new Uint32Array(buf);
}
然而unpackL('htk1')
給我Uint32Array [ 7602280, 3211371 ]
。
哇哦謝謝你的超快速回復!我記得你,因爲你幫助我用'?raw = 1'來保存img src表格中的dropbox api圖片。感謝你! – Noitidart