python binascii 进制转换实例
如下所示:
#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内置函数: hex(): #把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():把十进制整形转换成二进制字符 #把十进制整型转换成二进制 >>> bin(88) '0b1011000' >>> bin(33) '0b100001' oct():把十进制转换成八进制字符 #把十进制转换成八进制 >>> oct(500) '0764' >>> oct(488) '0750' chr():把一个整形转换成ASCII码表中对应的单个字符 #把一个整形转换成ASCII码表中对应的单个字符 >>> chr(98) 'b' >>> chr(97) 'a' ord():和chr相反,把ASCII码表中的字符转换成对应的整形 >>> ord('b') 98 >>> ord('c') 99
相关推荐
Oraclezcy 2011-07-17
stulen 2020-08-15
86206132 2020-04-09
阿喵阿汪 2019-06-12
88540591 2011-12-06
Ashes 2019-06-30
azhedashuaibi 2019-06-28
moyebaobei 2015-01-05
gallon00 2012-10-31
weii 2015-07-14
uglygirl 2015-03-19
87407607 2013-08-20
JohnnyPig 2013-05-13
MySQLl 2019-04-09
hwj 2010-02-19
pythontty 2018-10-26
88520499 2018-10-26
zhangwei0php 2013-05-10
Rio说 2018-01-26