base = [str(x) for x inrange(10)] + [chr(x) for x inrange(ord('A'), ord('A')+6)]
# 十进制 to 二进制 defdec2bin(string_num): num = int(string_num) mid = [] whileTrue: if num == 0: break num, rem = divmod(num, 2) mid.append(base[rem]) return''.join([str(x) for x in mid[::-1]]) # bin(string_num)
# 十进制 to 十六进制 defdec2hex(string_num): num = int(string_num) if num == 0: return'0' mid = [] whileTrue: if num == 0: break num, rem = divmod(num, 16) mid.append(base[rem]) return''.join([str(x) for x in mid[::-1]]) # return hex(string_num)
# 二进制 to 十进制 defbin2dec(string_num): returnstr(int(string_num, 2))
# 二进制 to 十六进制 defbin2hex(string_num): return dec2hex(bin2dec(string_num)) # hex(int(string_num, 2))