[英]Implement JavaScript Int32Array array in Python
我正在嘗試在 Python 中實現類似Int32Array
的東西。 我目前有兩個問題:
使用 Chrome 控制台,我得到以下結果:
var int32 = new Int32Array(2);
int32[0] = 12345678901234567890
Int32Array(2):
[-350287872, 0]
var int16 = new Int16Array(2);
int16[0] = 12345678901234567890
Int16Array(2):
[2048, 0]
int16[1] = 12345678901234567890
Int16Array(2):
[2048, 2048]
當我嘗試使用to_bytes(4)
function 轉換大 integer 時,出現錯誤:
>>> (12345678901234567890).to_bytes(4)
AttributeError: 'long' object has no attribute 'to_bytes'
有任何想法嗎?
將 int32 轉換為字節
offset_int =6422915
offset_bytes = offset_int.to_bytes(4, 'little')
print(offset_bytes)
#return b'\x83\x01b\x00'
將字節轉換為 int32
# declaring byte value
byte_val = b'\x83\x01b\x00'
# converting to int
# byteorder is big where MSB is at start
int_val = int.from_bytes(byte_val, "little")
print(int_val)
# return 6422915
如果你想要 Int16 到字節
offset_int =65535
offset_bytes = offset_int.to_bytes(2, 'little')
print(offset_bytes)
#return b'\xff\xff'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.