简体   繁体   English

在 Python 2.7 中将字符串更改为字节类型

[英]Changing string to byte type in Python 2.7

In python 3.2, i can change the type of an object easily.在 python 3.2 中,我可以轻松更改对象的类型。 For example :例如 :

x=0
print(type (x))
x=bytes(0)
print(type (x))

it will give me this :它会给我这个:

<class 'int'>
<class 'bytes'>

But, in python 2.7, it seems that i can't use the same way to do it.但是,在 python 2.7 中,似乎我不能使用相同的方法来做到这一点。 If i do the same code, it give me this :如果我执行相同的代码,它会给我这个:

<type 'int'>
<type 'str'>

What can i do to change the type into a bytes type?我该怎么做才能将类型更改为字节类型?

You are not changing types, you are assigning a different value to a variable.您不是在更改类型,而是为变量分配了不同的值。

You are also hitting on one of the fundamental differences between python 2.x and 3.x;您还发现了 python 2.x 和 3.x 之间的根本区别之一; grossly simplified the 2.x type unicode has replaced the str type, which itself has been renamed to bytes .大大简化了 2.x 类型unicode取代了str类型,它本身已重命名为bytes It happens to work in your code as more recent versions of Python 2 have added bytes as an alias for str to ease writing code that works under both versions.它恰好适用于您的代码,因为较新版本的 Python 2 添加了bytes作为str的别名,以简化编写可在两个版本下运行的代码。

In other words, your code is working as expected.换句话说,您的代码按预期工作。

What can i do to change the type into a bytes type?我该怎么做才能将类型更改为字节类型?

You can't, there is no such type as 'bytes' in Python 2.7.你不能,Python 2.7 中没有“字节”这样的类型。

From the Python 2.7 documentation (5.6 Sequence Types): "There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects."来自 Python 2.7 文档(5.6 序列类型):“有七种序列类型:字符串、Unicode 字符串、列表、元组、字节数组、缓冲区和 xrange 对象。”

From the Python 3.2 documentation (5.6 Sequence Types): "There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects."来自 Python 3.2 文档(5.6 序列类型):“有六种序列类型:字符串、字节序列(字节对象)、字节数组(字节数组对象)、列表、元组和范围对象。”

In Python 2.x, bytes is just an alias for str , so everything works as expected.在 Python 2.x 中, bytes只是str的别名,所以一切都按预期进行。 Moreover, you are not changing the type of any objects here – you are merely rebinding the name x to a different object.此外,您不会在此处更改任何对象的类型——您只是将名称x重新绑定到不同的对象。

May be not exactly what you need, but when I needed to get the decimal value of the byte d8 (it was a byte giving an offset in a file) i did:可能不完全是您需要的,但是当我需要获取字节 d8 的十进制值(它是一个在文件中给出偏移量的字节)时,我做了:

a = (data[-1:])          # the variable 'data' holds 60 bytes from a PE file, I needed the last byte
                         #so now a == '\xd8'  , a string
b = str(a.encode('hex')) # which makes b == 'd8' , again a string
c = '0x' + b             # c == '0xd8' , again a string
int_value = int(c,16)    # giving me my desired offset in decimal: 216

                         #I hope this can help someone stuck in my situation

Just example to emphasize a procedure of turning regular string into binary string and back:只是一个例子来强调将常规字符串转换为二进制字符串并返回的过程:

sb = "a0" # just string with 2 characters representing a byte
ib = int(sb, 16) # integer value (160 decimal)
xsb = chr(ib) # a binary string (equals '\xa0')

Now backwards现在倒退

back_sb = xsb.encode('hex')
back_sb == sb # returns True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM