#coding:utf-8 import binascii a = ‘worker‘ #先把worker转换成二进制数据然后在用十六进制表示 b = binascii.b2a_hex(a) print b #与b2a_hex相反 print binascii.a2b_hex(b) #这个功能和b2a_hex()一样 c = binascii.hexlify(a) print c #这个功能和a2b_hex()一样 print binascii.unhexlify(c) ######运行结果###### >>> 776f726b6572 worker 776f726b6572 worker
Python built-in functions:
Hex ():
| 12345678910111213 |
#把10进制转整形换成16进制>>> hex(88)‘0x58‘#把浮点型转换成16进制>>> 1.23.hex()‘0x1.3ae147ae147aep+0‘#内置函数hex和binascii.hexlify()的区别就在于,#hex只能接受整形不能接受字符串>>> hex(‘88‘)Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> hex(‘88‘)TypeError: hex() argument can‘t be converted to hexbin():ba |
Bin (): Converts decimal shaping to binary characters
| 12345 |
#把十进制整型转换成二进制>>> bin(88)‘0b1011000‘>>> bin(33)‘0b100001‘ |
Oct (): Convert decimal to octal characters
| 12345 |
#把十进制转换成八进制>>> oct(500)‘0764‘>>> oct(488)‘0750‘ |
Chr (): Converts a shape into a single character corresponding to the ASCII code table
| 12345 |
#把一个整形转换成ASCII码表中对应的单个字符>>> chr(98)‘b‘>>> chr(97)‘a‘ |
Ord (): In contrast to CHR, converts the characters in the ASCII code table into corresponding shaping
| 1234 |
>>> ord(‘b‘)98>>> ord(‘c‘)99 |
Reprinted from http://blog.51cto.com/zeping/1261876, thanks to bloggers, for personal study only
Python binary Conversion