Python 中的类型转换

发布于 2024-07-10 07:08:02 字数 94 浏览 10 评论 0原文

我需要将 Python 中的字符串转换为其他类型,例如无符号和有符号 8、16、32 和 64 位整数、双精度数、浮点数和字符串。

我怎样才能做到这一点?

I need to convert strings in Python to other types such as unsigned and signed 8, 16, 32, and 64 bit ints, doubles, floats, and strings.

How can I do this?

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

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

发布评论

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

评论(5

旧人九事 2024-07-17 07:08:02

您可以使用 int 函数将字符串转换为 32 位有符号整数:

string = "1234"
i = int(string)  # i is a 32-bit integer

如果字符串不表示整数,您将收到 ValueError 异常。 但请注意,如果字符串确实表示一个整数,但该整数不适合 32 位有符号 int,那么您实际上会得到一个 long 类型的对象。

然后,您可以通过一些简单的数学将其转换为其他宽度和符号:

s8 = (i + 2**7) % 2**8 - 2**7      # convert to signed 8-bit
u8 = i % 2**8                      # convert to unsigned 8-bit
s16 = (i + 2**15) % 2**16 - 2**15  # convert to signed 16-bit
u16 = i % 2**16                    # convert to unsigned 16-bit
s32 = (i + 2**31) % 2**32 - 2**31  # convert to signed 32-bit
u32 = i % 2**32                    # convert to unsigned 32-bit
s64 = (i + 2**63) % 2**64 - 2**63  # convert to signed 64-bit
u64 = i % 2**64                    # convert to unsigned 64-bit

您可以使用 float 函数将字符串转换为浮点数:

f = float("3.14159")

Python 浮点数是其他语言所称的 double,即它们是 64 位的。 Python 中没有 32 位浮点数。

You can convert a string to a 32-bit signed integer with the int function:

string = "1234"
i = int(string)  # i is a 32-bit integer

If the string does not represent an integer, you'll get a ValueError exception. Note, however, that if the string does represent an integer, but that integer does not fit into a 32-bit signed int, then you'll actually get an object of type long instead.

You can then convert it to other widths and signednesses with some simple math:

s8 = (i + 2**7) % 2**8 - 2**7      # convert to signed 8-bit
u8 = i % 2**8                      # convert to unsigned 8-bit
s16 = (i + 2**15) % 2**16 - 2**15  # convert to signed 16-bit
u16 = i % 2**16                    # convert to unsigned 16-bit
s32 = (i + 2**31) % 2**32 - 2**31  # convert to signed 32-bit
u32 = i % 2**32                    # convert to unsigned 32-bit
s64 = (i + 2**63) % 2**64 - 2**63  # convert to signed 64-bit
u64 = i % 2**64                    # convert to unsigned 64-bit

You can convert strings to floating point with the float function:

f = float("3.14159")

Python floats are what other languages refer to as double, i.e. they are 64-bits. There are no 32-bit floats in Python.

葬心 2024-07-17 07:08:02

Python 只有一个 int 类型。 要将字符串转换为 int,请使用 int(),如下所示:

>>> str = '123'
>>> num = int(str)
>>> num
123

编辑: 要转换为 float,请使用 float( ) 以完全相同的方式。

Python only has a single int type. To convert a string to an int, use int() like this:

>>> str = '123'
>>> num = int(str)
>>> num
123

Edit: Also to convert to float, use float() in the exact same way.

浪菊怪哟 2024-07-17 07:08:02

以下类型(大部分)在 Python 中一开始就不存在。 在 Python 中,字符串被转换为整数、长整型或浮点数,因为这就是全部。

您首先要求进行与 Python 无关的转换。 以下是您要求的类型及其 Python 等效类型的列表。

  • 无符号和有符号 int 8 位,int
  • 无符号和有符号 int 16 位,int
  • 无符号和有符号 int 32 位,无符号:long,有符号 < strong>int
  • 无符号和有符号 int 64 位,long

  • 双精度,< strong>float

  • float,float
  • string,这是你必须开始的

我不知道不知道下面是什么,所以我不知道 Python 的等价物。

  • 无符号和有符号 8 位、
  • 无符号和有符号 16 位、
  • 无符号和有符号 32 位、
  • 无符号和有符号 64 位。

您已经拥有所有重要的转换:int()long()float()

The following types -- for the most part -- don't exist in Python in the first place. In Python, strings are converted to ints, longs or floats, because that's all there is.

You're asking for conversions that aren't relevant to Python in the first place. Here's the list of types you asked for and their Python equivalent.

  • unsigned and signed int 8 bits, int
  • unsigned and signed int 16 bits, int
  • unsigned and signed int 32 bits, unsigned: long, signed int
  • unsigned and signed int 64 bits, long

  • double, float

  • float, float
  • string, this is what you had to begin with

I don't know what the following are, so I don't know a Python equivalent.

  • unsigned and signed 8 bit,
  • unsigned and signed 16 bit,
  • unsigned and signed 32 bit,
  • unsigned and signed 64 bit.

You already have all the conversions that matter: int(), long() and float().

野味少女 2024-07-17 07:08:02

我认为如果没有更多信息,这个问题不一定能得到很好的回答。 正如其他人所说,Python 中的整数只有 int 和 long ——该语言不遵守低级编程语言的位宽和符号原型。

如果您完全在 python 中操作,那么您可能会问错误的问题。 可能有更好的方法来满足您的需求。

例如,如果您正在与 C 代码或通过网络进行互操作,那么有方法可以做到这一点,并且看起来您之前发布的答案非常轻松地涵盖了该途径。

I don't think this can necessarily be answered well without more information. As others have said, there are only int and long for integers in python -- the language doesn't adhere to the bit-width and signedness archetypes of lower-level programming languages.

If you're operating completely within python, then you're probably asking the wrong question. There's likely a better way to do what you need.

If you are interoperating with, for instance, C code, or over the network, then there are ways to do this, and it looks like the answer to your previous posting covered that avenue pretty handily.

狼亦尘 2024-07-17 07:08:02

我刚刚遇到一个问题,我从 modbus 中以 16 位带符号的二进制补码形式传递了一个值。
我需要将其转换为带符号的数字。
我最终写了这个,看起来效果很好。

# convert a 32 bit (prob) integer as though it was 
# a 16 bit 2's complement signed one
def conv_s16(i):
    if (i & 0x8000):
        s16 = -(((~i) & 0xFFFF) + 1)
    else:
        s16 = i
    return s16

I just now had a problem where I had a value passed as a 16 bit signed twos complement number from modbus.
I needed to convert this to a signed number.
I ended up writing this, which seems to work fine.

# convert a 32 bit (prob) integer as though it was 
# a 16 bit 2's complement signed one
def conv_s16(i):
    if (i & 0x8000):
        s16 = -(((~i) & 0xFFFF) + 1)
    else:
        s16 = i
    return s16
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文