我试图确保我拥有的一些数据被编码为大端二进制。我正在使用 struct 模块来执行此操作。但是,我将两者都转换为大端或小端的结果是相同的。为什么?
import sys
import json
import struct
data = {
"val1": 20,
"val2": 24
}
b = json.dumps(data, ensure_ascii=False).encode("utf-8")
little = struct.pack(f"<{len(b)}s", b)
big = struct.pack(f">{len(b)}s", b)
print(f"System byteorder: {sys.byteorder}")
print(f"data:\t{b}")
print(f"little:\t{little}")
print(f"big:\t{big}")
print((big == little) and (little == b))
val = 25
b = bytes([val])
big = struct.pack(">H", val)
little = struct.pack("<H", val)
print()
print()
print(f"data:\t{b}")
print(f"little:\t{little}")
print(f"big:\t{big}")
print((big == little) and (little == b))
给出以下结果
System byteorder: little
data: b'{"val1": 20, "val2": 24}'
little: b'{"val1": 20, "val2": 24}'
big: b'{"val1": 20, "val2": 24}'
True
data: b'\x19'
little: b'\x19\x00'
big: b'\x00\x19'
False
大话西游666
相关分类