我的python菜鸟之路10
------------------------------------内置函数----------------------------------
进制转换相关
- bin ,将十进制转换为二进制(0b)
- oct ,将十进制转换为八进制(0o)
- hex,将十进制转化为十六进制
- int,将其他进制转化为十进制(0x)
#将二进制转化为十进制 num = ‘0b1010111011‘ v1 = int(num,base=2) print(v1) #将十六进制转化十进制 num = ‘0x15646351‘ v2 = int(num,base=16) print(v2)
将IP地址转化为二进制
ip=‘192.168.12.79‘ v1 = ip.split(‘.‘) str = [] for i in v1: num = bin(int(i)) str.append(num) a=‘,‘.join(str) print(a) ‘‘‘注意: 1.join后的函数里面的小元素必须是字符型 2.默认二进制位字符型 ‘‘‘
- IP相关练习题
ip=‘192.168.12.79‘ v1 = ip.split(‘.‘) strl = [] for i in v1: num = bin(int(i)) if len(num) == 10: num = num[2:10] elif len(num) < 10: v1 = 10-len(num) num =‘0‘*v1+num[2::] er_num=str(int(num,base=2)) strl.append(er_num) result=",".join(strl) print(result)
相关推荐
lsfreeing 2020-05-05
哈嘿Blog 2020-10-26
明月清风精进不止 2020-07-05
PythonMaker 2020-07-05
kkpiece 2020-06-16
qscool 2020-06-12
CloudXli 2020-06-11
vs00ASPNET 2020-06-09
Dimples 2020-06-08
kuoying 2020-06-07
JJandYY 2020-05-31
Wyt00 2020-05-30
liuyh 2020-04-03
CloudXli 2020-05-11
世樹 2020-05-11
bizercsdn 2020-05-10
joyjoy0 2020-05-09