Files
python3-cookbook/cookbook/c06/p09_codec_hex.py
2014-10-18 15:33:17 +08:00

31 lines
527 B
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 编码/解码十六进制原始字符串
Desc :
"""
import binascii
import base64
def codec_hex():
s = b'hello'
# Encode as hex
h = binascii.b2a_hex(s)
print(h)
# Decode back to bytes
h = binascii.a2b_hex(h)
print(h)
# 使用base64模块也可以
h = base64.b16encode(s)
print(h)
print(h.decode('ascii'))
h = base64.b16decode(h)
print(h)
print(h.decode('ascii'))
if __name__ == '__main__':
codec_hex()