Python 中的类型转换
我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
int
函数将字符串转换为 32 位有符号整数:如果字符串不表示整数,您将收到
ValueError
异常。 但请注意,如果字符串确实表示一个整数,但该整数不适合 32 位有符号 int,那么您实际上会得到一个long
类型的对象。然后,您可以通过一些简单的数学将其转换为其他宽度和符号:
您可以使用
float
函数将字符串转换为浮点数:Python 浮点数是其他语言所称的
double
,即它们是 64 位的。 Python 中没有 32 位浮点数。You can convert a string to a 32-bit signed integer with the
int
function: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 typelong
instead.You can then convert it to other widths and signednesses with some simple math:
You can convert strings to floating point with the
float
function:Python floats are what other languages refer to as
double
, i.e. they are 64-bits. There are no 32-bit floats in Python.Python 只有一个
int
类型。 要将字符串转换为int
,请使用int()
,如下所示:编辑: 要转换为 float,请使用
float( )
以完全相同的方式。Python only has a single
int
type. To convert a string to anint
, useint()
like this:Edit: Also to convert to float, use
float()
in the exact same way.以下类型(大部分)在 Python 中一开始就不存在。 在 Python 中,字符串被转换为整数、长整型或浮点数,因为这就是全部。
您首先要求进行与 Python 无关的转换。 以下是您要求的类型及其 Python 等效类型的列表。
无符号和有符号 int 64 位,long
双精度,< strong>float
我不知道不知道下面是什么,所以我不知道 Python 的等价物。
您已经拥有所有重要的转换:
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 64 bits, long
double, float
I don't know what the following are, so I don't know a Python equivalent.
You already have all the conversions that matter:
int()
,long()
andfloat()
.我认为如果没有更多信息,这个问题不一定能得到很好的回答。 正如其他人所说,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.
我刚刚遇到一个问题,我从 modbus 中以 16 位带符号的二进制补码形式传递了一个值。
我需要将其转换为带符号的数字。
我最终写了这个,看起来效果很好。
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.