1、bytes、bytearray
---Python3 引入的!
bytes:不可變字節序列,bytearray:字節屬組,可變
都是連續的空間。
2、字符串與bytes
字符串是字符組成的有序的序列,字符可以使用編碼來理解
bytes 是戒子組成的有序的不可變序列
bytearray 是戒子組成的有序的可變序列
3、編碼、解碼
字符串按照不同的字符集編碼encode返回字節序列bytes
encode(encoding = ‘utf-8', errors = 'strict') ---> bytes
1 1 In [139]: 'abd'.encode() # 默認是utf-8 2 2 Out[139]: b'abd' # 應該是數字,但是為了讓人看,所以顯示成這樣,所以前面有一個b
字節序列按照不同的字符集解碼decode返回字符串、
bytes.decode(encoding='utf-8' , errors= 'strict' ) ---> str
bytearray.decode(encoding= 'utf-8',errors = 'strict') ---> str
1 In [140]: print(b'abd') 2 b'abd' 3 4 In [141]: _.decode() # _ 表示上次的結果 5 Out[141]: 'abd'
ASCII:American Standard Code for Information
3、bytes定義:
定義:
bytes()空bytes
bytes(int)指定字節的bytes,被0 填充(0是ASCII 0)
In [142]: bytes(5) Out[142]: b'\x00\x00\x00\x00\x00'
bytes(iterable_of_ints) --> bytes [0-255]的int組成的可迭代對象
bytes(string, encoding[, errors]) ---> bytes等價於string,encode()
bytes(bytes_or_buffer) ---> immutable copy of bytes_or_buffer 從一個字節序列或buffer中復制一個新的不可變的bytes對象。
使用b 前綴定義:
只允許基本的ASCII使用字符形式b'abc9'
使用16進制表示b"\x41\x61" 字符串就用 \x41 數字 0x61

1 In [158]: a = bytes(7) # 定義一個字節長度,用ASCII的十六進制的0填充 2 3 In [160]: a 4 Out[160]: b'\x00\x00\x00\x00\x00\x00\x00' 5 6 ------------------------------------------------------------------------------ 7 8 In [161]: c = bytes(range(10)) # 創建一個字節類型,從0-9 9 10 In [162]: c 11 Out[162]: b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'# python為了讓人看懂,所以顯示如上所示,如:x09 對ASCII中的 \t, 而其他的對應的字符無法表示,所以以源字節碼顯示。 12 13 ------------------------------------------------------------------------------ 14 15 In [163]: d = bytes('efg',encoding='utf-8') #這樣用很少,一般都是string.encoding()---默認是utf-8編碼 16 17 In [164]: d 18 Out[164]: b'efg' 19 20 In [165]: e = bytes(d) 相當於copy了一份 21 22 In [166]: e 23 Out[166]: b'efg'
bytes操作:
和 str 類型類似,都是不可變,所以方法很多都一樣,只不過bytes方法,輸入是bytes,輸出是bytes
b'abcdef'.replace(b'f', b'k')
b'abc'.find(b'b')
類方法 bytes,formhex(string)
string 必須是2個字符的16進制的形式,'1662 6a 6b' ,空格將被忽略!
1 In [172]: bytes.fromhex('616263 09 6a 6b00') 2 Out[172]: b'abc\tjk\x00' # 忽略空格,00沒法顯示,所以用\x00表示
索引:b'abcde'[2] 返回該字節對應的數,int類型
1 In [173]: b'abcde'[2] 2 Out[173]: 99 3 4 In [174]: 'abc'.encode() 5 Out[174]: b'abc' 6 7 In [175]: 'abc'.encode()[1] 8 Out[175]: 98
4、bytearray定義
定義:
bytearray() 空bytearray
bytearray(int) 指定字節的bytearray ,被0 填充
bytearray(iterable_of_ints) --> bytearray [0,255] de int 組成的可迭代對象
bytearray(string, encoding[, errors]) ---> bytearray 近似string.encode(),(但是這個是返回的字節類型),返回可變對象
bytearray( bytes_or_buffer) 從一個字節序列或buffer復制出一個新的可變的bytearray對象
注: b前綴定義的類型是bytes類型
操作:
和bytes類型的方法相同:
bytearray(b'abcd').replace(b'f',b'k')
bytearrya(b'abc').find(b'b')
類方法bytearray.fromhex(string)
sting 必須是2個字符的16進制的形式,'6162 6a6vb' ,空格將被忽略!
In [176]: bytearray.fromhex('6261 6a') Out[176]: bytearray(b'baj')
hex():返回16進制表示的字符串
1 In [177]: bytearray('abc'.encode()).hex() 2 Out[177]: '616263'
索引:bytearray(b'abcdeff')[2] 返回該字節對應的數,int類型
其他操作:

1 In [205]: a 2 Out[205]: bytearray(b'asdfa') 3 4 In [206]: a.append(0x61) 5 6 In [207]: a.append(91) 7 8 In [208]: a 9 Out[208]: bytearray(b'asdfaa[') 10 11 In [209]: a.insert(0,91) 12 13 In [210]: a 14 Out[210]: bytearray(b'[asdfaa[') 15 16 In [211]: a.extend(range(2)) 17 18 In [212]: a 19 Out[212]: bytearray(b'[asdfaa[\x00\x01') 20 21 In [213]: a.pop() 22 Out[213]: 1 23 24 In [214]: a 25 Out[214]: bytearray(b'[asdfaa[\x00') 26 27 In [217]: a.remove(97) 28 29 In [218]: a 30 Out[218]: bytearray(b'[sdfaa[\x00') 31 32 In [220]: a.reverse() 33 34 In [221]: a 35 Out[221]: bytearray(b'\x00[aafds[') 36 37 In [222]: a.clear 38 Out[222]: <function bytearray.clear()> 39 40 In [223]: a.clear() 41 42 In [224]: a 43 Out[224]: bytearray(b'')
注:上述方法使用int,且在【0-255】 之間
5、int 和 bytes
int,from_bytes( bytes,byteordes)
將一個字節數組表示成整數
int.to_bytes(length, byteorder)
byteorder 字節序
將一個整數表示成一個指定長度的字節數組
str-----> bytes ---> int:
1 In [227]: i = int.from_bytes(b'abc','big') 2 3 In [228]: i 4 Out[228]: 6382179 5 6 In [229]: hex(i) 7 Out[229]: '0x616263' 8 9 10 In [231]: i.to_bytes(3,'big') # 數字只能大於等於長度,否則報錯 11 Out[231]: b'abc' 12 13 In [232]: i.to_bytes(5,'big') # 大於就補0 14 Out[232]: b'\x00\x00abc'
大端模式,小端模式:
mac:大端模式
network:大端模式
Windows:小端模式
字節序:b'\x61\x62' 61應該放在高地址,還是低地址,如果低字節放在高地址,就是大端模式,即 big