xirongxudlut 2020-06-28
#将二进制转化为十进制 num = ‘0b1010111011‘ v1 = int(num,base=2) print(v1) #将十六进制转化十进制 num = ‘0x15646351‘ v2 = int(num,base=16) print(v2)
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)