程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Conversion of string (hexadecimal and regular) and byte stream in Python

編輯:Python

Source code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import binascii
def test():
print(" Method 1 processing ...")
db = b'859859843905894-000-\x34\x33\x32\x31\x30-ABC-abc'
#2 Hexadecimal byte flow 16 Hexadecimal visible string to
dhs = str(binascii.b2a_hex(db), 'utf-8').upper()
print(dhs, type(dhs))
#16 Hexadecimal string to 2 Binary byte stream
db = binascii.a2b_hex(dhs)
print(db, type(db))
# Byte flow utf-8 character string
das = str(db, 'utf-8')
print(das, type(das))
#utf-8 String to byte stream
db = bytes(das, 'utf-8')
print(db, type(db))
print("\n\n Method 2 ...")
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")

Running results :

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved