2024-06-26 08:28:04 发布
网友
>>> import binascii, struct >>> foo_hex = 'b1e92555' >>> foo_bin = binascii.unhexlify(foo_hex) >>> int(foo_hex, 16) 2984846677 >>> struct.unpack('i', foo_bin)[0] 1428548017
为什么这些整数不同?哪一种方法是正确的,如何将另一种方法更改为正确的?你知道吗
struct是用小尾数表示法来解释这个值,而用unhexlify和int(.., 16)则把它解释为一个大尾无符号整数。你知道吗
struct
unhexlify
int(.., 16)
使用<或>指定输入的endianness,并使用I将其解释为有符号int:
<
>
I
>>> struct.unpack('>I', foo_bin)[0] 2984846677 >>> struct.unpack('<I', foo_bin)[0] 1428548017
请参阅struct文档页的Byte Order, Size and Alignment section。你知道吗
struct
是用小尾数表示法来解释这个值,而用unhexlify
和int(.., 16)
则把它解释为一个大尾无符号整数。你知道吗使用
<
或>
指定输入的endianness,并使用I
将其解释为有符号int:请参阅
struct
文档页的Byte Order, Size and Alignment section。你知道吗相关问题 更多 >
编程相关推荐