Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแปลง Byte Literals เป็น Python Strings

ต้อง decode bytes ให้เป็น string

1. ใช้ decode() บน bytes object

>> b"abcde".decode("utf-8")
'abcde'

2. ใช้ map(chr, bytes) สำหรับ ASCII

bytes = [112, 52, 52]
print("".join(map(chr, bytes)))

ผลลัพธ์

p44