python中unicode字符串的转换
我需要将Python中的unicode字符串转换为其他类型,例如无符号和有符号int 8位,无符号和有符号int 16位,无符号和有符号int 32位,无符号和有符号int 64位,双精度,浮点,字符串,无符号和有符号8 位、无符号和有符号 16 位、无符号和有符号 32 位、无符号和有符号 64 位。
我需要你们的帮助。
I need to convert unicode strings in Python to other types such as unsigned and signed int 8 bits,unsigned and signed int 16 bits,unsigned and signed int 32 bits,unsigned and signed int 64 bits,double,float,string,unsigned and signed 8 bit,unsigned and signed 16 bit, unsigned and signed 32 bit,unsigned and signed 64 bit.
I need help from u people.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
int()
将字符串转换为整数。 Python 没有不同的固定宽度整数,所以你只能得到一种类型的东西。然后使用
struct
将整数打包成固定宽度:struct
生成一个包含二进制数字的字节字符串。编辑:
从评论来看,您似乎只想将字符串(十进制数字)转换为整数。 只需使用 int() 即可,但是您不会获得指定类型的所有复杂的上溢/下溢语义。 你无法在 python 中重现这一点,至少在不编写大量代码的情况下是这样。
我认为如果您需要更多帮助,您必须更准确地了解您想要实现的目标。
use
int()
to convert the string to an integer. Python doesn't have different fixed-width integers so you'll just get one type of thing out.Then use
struct
to pack the integer into a fixed width:struct
produces a byte-string containing the number in binary.EDIT:
From the comments it sounds like you just want to convert the string (of decimal digits) into an integer. Just use
int()
for that, however you won't get all the complicated overflow/underflow semantics of the specified types. You can't reproduce that in python, at least not without writing a whole lot of code.I think if you want any more help you'll have to be more precise about what you want to achieve.