源碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import binascii
def test():
print("方法一處理...")
db = b'859859843905894-000-\x34\x33\x32\x31\x30-ABC-abc'
#2進制字節流轉16進制可見字符串轉
dhs = str(binascii.b2a_hex(db), 'utf-8').upper()
print(dhs, type(dhs))
#16進制字符串轉2進制字節流
db = binascii.a2b_hex(dhs)
print(db, type(db))
#字節流轉utf-8字符串
das = str(db, 'utf-8')
print(das, type(das))
#utf-8字符串轉字節流
db = bytes(das, 'utf-8')
print(db, type(db))
print("\n\n方法二處理...")
db = b'859859843905894-000-\x34\x33\x32\x31\x30-ABC-abc'
print(db, type(db))
das = db.decode('ascii')
print(das, type(das))
dus = db.decode('utf-8')
print(dus, type(dus))
dab = das.encode('ascii')
dub = dus.encode('utf-8')
print(dab, type(dab))
print(dub, type(dub))
if __name__ == '__main__':
print("begin...")
test()
print("end")
運行結果: