从“无符号”值读取 32 位有符号值字节流

发布于 2025-01-04 06:38:09 字数 426 浏览 7 评论 0原文

我想从一个文件中提取数据,该文件的信息以大端存储并且始终无符号。从unsigned intint“cast”如何影响实际的十进制值?我是否正确,最左边的位决定该值是正数还是负数?

我想用 python 解析该文件格式,并且读取和无符号值很容易:

def toU32(bits):
    return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8  | ord(bits[3])

但是相应的 toS32 函数是什么样子的?


感谢您提供有关 struct 模块的信息。但我仍然对我的实际问题的解决方案感兴趣。

I want to extract data from a file whoose information is stored in big-endian and always unsigned. How does the "cast" from unsigned int to int affect the actual decimal value? Am I correct that the most left bit decides about the whether the value is positive or negative?

I want to parse that file-format with python, and reading and unsigned value is easy:

def toU32(bits):
    return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8  | ord(bits[3])

but how would the corresponding toS32 function look like?


Thanks for the info about the struct-module. But I am still interested in the solution about my actual question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁虫ゝ 2025-01-11 06:38:09

我会使用struct

import struct

def toU32(bits):
    return struct.unpack_from(">I", bits)[0]

def toS32(bits):
    return struct.unpack_from(">i", bits)[0]

格式字符串“>I”表示从字符串位读取大端字节序“>”、无符号整数“I”。对于有符号整数,您可以使用“>i”。

编辑

必须查看另一个StackOverflow答案来记住如何“转换”有符号整数python 中的无符号整数。尽管它不是转换,而是重新解释这些位。

import struct

def toU32(bits):
        return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8  | ord(bits[3])

def toS32(bits):
    candidate = toU32(bits);
    if (candidate >> 31): # is the sign bit set?
        return (-0x80000000 + (candidate & 0x7fffffff)) # "cast" it to signed
    return candidate


for x in range(-5,5):
    bits = struct.pack(">i", x)
    print toU32(bits)
    print toS32(bits)

I would use struct.

import struct

def toU32(bits):
    return struct.unpack_from(">I", bits)[0]

def toS32(bits):
    return struct.unpack_from(">i", bits)[0]

The format string, ">I", means read a big endian, ">", unsigned integer, "I", from the string bits. For signed integers you can use ">i".

EDIT

Had to look at another StackOverflow answer to remember how to "convert" a signed integer from an unsigned integer in python. Though it is less of a conversion and more of reinterpreting the bits.

import struct

def toU32(bits):
        return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8  | ord(bits[3])

def toS32(bits):
    candidate = toU32(bits);
    if (candidate >> 31): # is the sign bit set?
        return (-0x80000000 + (candidate & 0x7fffffff)) # "cast" it to signed
    return candidate


for x in range(-5,5):
    bits = struct.pack(">i", x)
    print toU32(bits)
    print toS32(bits)
怎言笑 2025-01-11 06:38:09

我将使用 struct 模块的 打包和解包方法。

有关一些示例,请参阅 Python 中整数的字节序

I would use the struct module's pack and unpack methods.

See Endianness of integers in Python for some examples.

暮年慕年 2025-01-11 06:38:09

toS32(bits) 的无条件版本可能类似于:

def toS32(bits):
    decoded = toU32(bits)
    return -(decoded & 0x80000000) + (decoded & 0x7fffffff)

当然,您也可以预先计算任何其他位大小的掩码。

The non-conditional version of toS32(bits) could be something like:

def toS32(bits):
    decoded = toU32(bits)
    return -(decoded & 0x80000000) + (decoded & 0x7fffffff)

You can pre-compute the mask for any other bit size too of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文