莆田有哪幾家做網(wǎng)站設(shè)計(jì)關(guān)鍵詞優(yōu)化搜索排名
在Python中,bytearray
?是一個(gè)可變序列,用于表示一個(gè)字節(jié)數(shù)組。與不可變的?bytes
?類型相比,bytearray
?允許你修改其內(nèi)容。你可以通過索引來訪問和修改?bytearray
?中的元素,也可以添加或刪除元素。
使用?bytearray
?的一些示例:
# 創(chuàng)建一個(gè)空的 bytearrayba = bytearray()print(ba) # 輸出: bytearray(b'')# 創(chuàng)建一個(gè)包含初始值的 bytearrayba = bytearray([0x11, 0x22, 0x33])print(ba) # 輸出: bytearray(b'\x11\x22\x33')# 訪問 bytearray 中的元素print(ba[0]) # 輸出: 17(0x11 的十進(jìn)制表示)# 修改 bytearray 中的元素ba[1] = 0x44print(ba) # 輸出: bytearray(b'\x11D\x33'),其中 'D' 是 0x44 的 ASCII 字符# 添加元素到 bytearray 的末尾ba.append(0x55)print(ba) # 輸出: bytearray(b'\x11D\x33U'),其中 'U' 是 0x55 的 ASCII 字符# 從 bytearray 中刪除元素del ba[2]print(ba) # 輸出: bytearray(b'\x11D\x55')# 將 bytes 轉(zhuǎn)換為 bytearrayb = b'\x01\x02\x03'ba = bytearray(b)print(ba) # 輸出: bytearray(b'\x01\x02\x03')# 將 bytearray 轉(zhuǎn)換為 bytesb = bytes(ba)print(b) # 輸出: b'\x01\x02\x03'
在上面的示例中,你可以看到如何使用?bytearray
?來存儲(chǔ)和操作字節(jié)序列。這對(duì)于需要頻繁修改字節(jié)數(shù)據(jù)的場景特別有用,因?yàn)?bytes
?類型是不可變的,每次修改都需要?jiǎng)?chuàng)建一個(gè)新的?bytes
?對(duì)象。而?bytearray
?允許你原地修改數(shù)據(jù),從而提高性能。
數(shù)組的長度:
# 創(chuàng)建一個(gè)bytearray
app_buf = bytearray([0x11, 0x22, 0x33]) # 輸出bytearray的長度
print(len(app_buf)) # 輸出: 3
數(shù)組打印
注意:如果數(shù)組里的內(nèi)容為 字符直接用print 打印就會(huì)直接打印成字符!例如
ba[1] = 0x44? ?print(ba) # 輸出: bytearray(b'\x11D\x33'),其中 'D' 是 0x44 的 ASCII 字符
十六進(jìn)制方式打印數(shù)組
要以十六進(jìn)制的方式打印bytearray
中的每個(gè)字節(jié),你可以使用列表推導(dǎo)式(list comprehension)結(jié)合format
函數(shù)或f-string(格式化字符串字面量)來格式化每個(gè)字節(jié)。以下是一些示例:
使用循環(huán)和直接打印:
app1_buf = bytearray([0x11,0x23,0x63])
for byte in app1_buf:print(f"{byte:02X}", end=' ')
使用列表推導(dǎo)式和format
函數(shù):
app_buf = bytearray([0x11, 0x22, 0x33])
hex_values = ['{:02X}'.format(byte) for byte in app_buf]
print(' '.join(hex_values)) # 輸出: 11 22 33
使用列表推導(dǎo)式和f-string(Python 3.6+):
app_buf = bytearray([0x11, 0x22, 0x33])hex_values = [f'{byte:02X}' for byte in app_buf]print(' '.join(hex_values)) # 輸出: 11 22 33
在這兩個(gè)示例中,我們都創(chuàng)建了一個(gè)列表推導(dǎo)式來遍歷bytearray
中的每個(gè)字節(jié),并使用format
函數(shù)或f-string將其格式化為兩位的十六進(jìn)制字符串。然后,我們使用join
方法將這些字符串連接成一個(gè)用空格分隔的字符串,并打印出來。
十進(jìn)制打印數(shù)組
如果你想要以十進(jìn)制的方式打印bytearray
中的每個(gè)字節(jié),你可以直接遍歷bytearray
并打印每個(gè)字節(jié)的值,因?yàn)镻ython在默認(rèn)情況下會(huì)以十進(jìn)制打印小的整數(shù)。但是,如果你想要確保它們是作為十進(jìn)制數(shù)明確顯示的(特別是當(dāng)與其他數(shù)據(jù)混合時(shí)),你可以使用format
函數(shù)或f-string來指定格式。
以下是一些示例:
使用循環(huán)和直接打印:
app_buf = bytearray([0x11, 0x22, 0x33])
for byte in app_buf:
print(byte) # 直接打印,默認(rèn)為十進(jìn)制
# 輸出:
# 17
# 34
# 51
使用列表推導(dǎo)式和format
函數(shù)(雖然在這種情況下可能不是必需的,但為了一致性):
app_buf = bytearray([0x11, 0x22, 0x33])
decimal_values = ['{}'.format(byte) for byte in app_buf]
print(' '.join(decimal_values)) # 使用' '連接成字符串
# 輸出: 17 34 51
使用列表推導(dǎo)式和f-string:
app_buf = bytearray([0x11, 0x22, 0x33])decimal_values = [f'{byte}' for byte in app_buf]print(' '.join(decimal_values)) # 使用' '連接成字符串# 輸出: 17 34 51
在這些示例中,你可以看到如何遍歷bytearray
中的每個(gè)字節(jié),并直接打印它們(默認(rèn)為十進(jìn)制),或者使用列表推導(dǎo)式和字符串格式化來創(chuàng)建一個(gè)包含十進(jìn)制值的字符串列表,然后將其連接成一個(gè)用空格分隔的字符串。