Home > Backend Development > Python Tutorial > 使用Python内置的模块与函数进行不同进制的数的转换

使用Python内置的模块与函数进行不同进制的数的转换

WBOY
Release: 2016-06-10 15:05:52
Original
1441 people have browsed it

binascii 模块:
它包含一个把二进制数值转换成十六进制的函数,同样也可以反过来转。 #binary_value是二进制数值不是字符串,也不是int型的1010

binascii.b2a_hex(binary_value) ##binary_value 一般读二进制文件可以得到 
 
>>'89' <type str> 

Copy after login

python自带的builtin函数:
bin(num) 十进制数值 ===》二进制字符串

bin(10) 
 
>> '0b1010' <type, str> 

Copy after login

oct(num) 十进制数值 ===》八进制字符串

oct(10) 
 
>>'012' <type, str> 

Copy after login

hex(num) 十进制数值 ===》十六进制字符串

hex(20) 
 
>>'0x14' <type, str> 

Copy after login

int(str, base) 其它进制字符串 ===》十进制的数值,其中base代表str具体是属于哪个进制,如果是2则表示str是二进制, 默认base为十进制

int('20') 
>>20 <type, int> 
int('10', 2) 
>>2 <type, int> 
int('10', 8) 
>>8 <type, int> 
int('20', 10) 
>>20 <type, int> 
int('20',16) 
>>32 <type, int> 

Copy after login

字符与数字转换函数:
chr(int) 整型 转 字符

chr(65) 
 
>>'A', <type, str> 

Copy after login

ord(chr) 字符 转 整型

ord('a') 
 
>>97, <type, int> 

Copy after login

最后,给一个读取图片文件二进制内容的示例:

#!/usr/bin/env python  
#encoding: utf-8 
import binascii  
 
fh = open(r'C:\Temp\img\2012517165556.png', 'rb') 
a = fh.read() 
#print 'raw: ',`a`,type(a) 
hexstr = binascii.b2a_hex(a) #得到一个16进制的数 
#print 'hex: ',hexstr, type(hexstr) 
bsstr = bin(int(hexstr,16))[2:] 
print 'bin: ',bsstr, type(bsstr) 

Copy after login

1010刷屏的效果,是不是有点黑客帝国的赶脚啊,呵呵

2016312105808499.png (663×432)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template