------------------------------------内置函数----------------------------------
进制转换相关
- 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='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)