概述
0)encode, decode
print(type(‘struct模块’.encode(‘utf-8’)))
print(type(‘struct模块’.encode(‘gbk’)))
print(type(‘struct模块’.encode(‘gb2312’)))
print(type(str(‘struct模块’.encode(‘gb2312’))))
print(type(‘struct模块’.encode(‘gb2312’).decode(‘gb2312’)))
print(type(‘struct模块’))
<class ‘bytes’>
<class ‘bytes’>
<class ‘bytes’>
<class ‘str’>
<class ‘str’>
<class ‘str’>
aa.encode(a)表示此字符串编码为此码a,
aa.decode(b)表示此字符串采用码b进行解码,b必须是aa的码。
print(type(‘struct模块’.encode(‘gb2312’).decode(‘utf-8’)))是错误的
1)bytes、str
bytes是Python3.x新加的数据类型(在Python2.x中被合并在str)中
2)bytes()
bytes()是Python3的一个内置函数
准确的讲,Python没有提供专门处理字节的数据类型
但由于b’str_obj’可以表示bytes_obj
所以,字节数组 <=> 二进制str
C语言中,我们可以很方便地用struct、union来处理字节,以及字节和int、float的转换
你可以使用位运算把其他数据类型转换为字节类型。
s1 = b’\xf1\xff’
print(int.from_bytes(s1, byteorder=‘big’, signed