Python有字節編碼庫binascii
import binascii
#創建字節數組,數組長度必須爲偶數,因爲兩個字符爲一個字節
a = b'helloworld'
print(a)
#字節數組轉換爲十六進制字符串 !!!的字節數組,注意最後,返回值還是字節數組,如果需要轉字符串需要decode
print(binascii.b2a_hex(a))
print(binascii.b2a_hqx(a))
#字節數組轉換爲BASE64字符串的字節數組
print(binascii.b2a_base64(a))
#字節數組轉換爲十六進制字符串的字節數組
print(binascii.hexlify(a))
print(binascii.b2a_hex(a).decode("utf-8"))
print(binascii.b2a_hqx(a).decode("utf-8"))
print(binascii.b2a_base64(a).decode("utf-8"))
print(binascii.hexlify(a).decode("utf-8"))
可以看到binascii函數的輸入輸出都是字節數組,轉換爲字符串需要decode、encode等