【问题标题】:How to convert hex string 'o\xf2\x00\x00' into a int32?如何将十六进制字符串 'o\xf2\x00\x00' 转换为 int32?
【发布时间】:2013-05-12 09:10:21
【问题描述】:

当我用 python 代码读取前 4 个字节时

len = fobj.read(4)

我收到了'o\xf2\x00\x00'。这应该是int32,当我用其他工具(例如matlab)读取文件时,它会给出62063

任何人都可以提供任何关于如何在 python 中将hex 字符串转换为int 的提示吗?

我已阅读 this hex string to int conversion discussion,,但没有找到任何帮助。

感谢您的任何建议!

【问题讨论】:

    标签: python int hex


    【解决方案1】:
    >>> import struct
    >>> struct.unpack('<i', 'o\xf2\x00\x00')
    (62063,)
    

    【讨论】:

    • @IgnacioVazquez-Abrams 现在明确采用小端序
    • 非常感谢 jamylak 的及时回复!
    【解决方案2】:

    我试过了,得到了答案:

    >>> print(int.from_bytes(b'o\xf2\x00\x00','little'))
    62063
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 2022-08-04
      • 2013-02-07
      • 1970-01-01
      • 2017-08-23
      • 2014-03-19
      相关资源
      最近更新 更多