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.
我需要将Python中的unicode字符串转换为其他类型,例如unsigned和signed int 8位,unsigned和signed int 16位,unsigned和signed int 32位,unsigned和signed int 64位,double,float,string,unsigned和signed 8位,无符号和有符号16位,无符号和带符号32位,无符号和带符号64位。
I need help from u people.
我需要你们的帮助。
1 个解决方案
#1
11
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.
使用int()将字符串转换为整数。 Python没有不同的固定宽度整数,所以你只需要输出一种类型的东西。
Then use struct
to pack the integer into a fixed width:
然后使用struct将整数打包成固定宽度:
res = struct.pack("=B",i) ## uint8_t
res = struct.pack("=b",i) ## int8_t
res = struct.pack("=H",i) ## uint16_t
res = struct.pack("=h",i) ## int16_t
res = struct.pack("=I",i) ## uint32_t
res = struct.pack("=i",i) ## int32_t
res = struct.pack("=Q",i) ## uint64_t
res = struct.pack("=q",i) ## int64_t
res = struct.pack("=f",i) ## float
res = struct.pack("=d",i) ## double
struct
produces a byte-string containing the number in binary.
struct生成一个包含二进制数字的字节字符串。
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.
编辑:从评论中听起来你只想将字符串(十进制数字)转换为整数。只需使用int(),但是您不会获得指定类型的所有复杂的上溢/下溢语义。您无法在python中重现它,至少在没有编写大量代码的情况下也是如此。
I think if you want any more help you'll have to be more precise about what you want to achieve.
我想如果你想要更多的帮助,你必须更准确地了解你想要达到的目标。
#1
11
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.
使用int()将字符串转换为整数。 Python没有不同的固定宽度整数,所以你只需要输出一种类型的东西。
Then use struct
to pack the integer into a fixed width:
然后使用struct将整数打包成固定宽度:
res = struct.pack("=B",i) ## uint8_t
res = struct.pack("=b",i) ## int8_t
res = struct.pack("=H",i) ## uint16_t
res = struct.pack("=h",i) ## int16_t
res = struct.pack("=I",i) ## uint32_t
res = struct.pack("=i",i) ## int32_t
res = struct.pack("=Q",i) ## uint64_t
res = struct.pack("=q",i) ## int64_t
res = struct.pack("=f",i) ## float
res = struct.pack("=d",i) ## double
struct
produces a byte-string containing the number in binary.
struct生成一个包含二进制数字的字节字符串。
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.
编辑:从评论中听起来你只想将字符串(十进制数字)转换为整数。只需使用int(),但是您不会获得指定类型的所有复杂的上溢/下溢语义。您无法在python中重现它,至少在没有编写大量代码的情况下也是如此。
I think if you want any more help you'll have to be more precise about what you want to achieve.
我想如果你想要更多的帮助,你必须更准确地了解你想要达到的目标。