【问题标题】:Way to get value of this hex number获取此十六进制数值的方法
【发布时间】:2010-08-17 08:08:34
【问题描述】:
import binascii

f = open('file.ext', 'rb')
print binascii.hexlify(f.read(4))
f.close()

打印出来:

84010100

我知道我必须从这些数据中检索十六进制数字 184。 如何在python中完成?我以前用过 struct 模块,但我不知道它是不是 little endian,big..whatever.. 如何使用 struct 从这个数字中得到 184?

【问题讨论】:

    标签: python binary


    【解决方案1】:
    >>> x = b'\x84\x01\x01\x00'
    >>> import struct
    >>> struct.unpack_from('<h', x)
    (388,)
    >>> map(hex, struct.unpack_from('<h', x))
    ['0x184']
    

    &lt; 表示小端,h 表示读取 16 位整数(“short”)。详情在package doc

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2015-08-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多