欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python:整数在二进制、八进制、十进制和十六进制之间转换

程序员文章站 2022-07-15 09:36:22
...

本文使用python中自带函数将一个整数在二进制、八进制、十进制和十六进制之间*转换。

    这个整数可能是个int,也可能是个string。

    用到了otc,hex,bin,format这四个函数,和int这个class。

上面几个函数的用法:

    hex(integer) 将integer转换为16进制,形式为0x0123456789abcdef。integer必须为整型。

    otc,bin类似。

    format(integer, 'x') 将integer转换为16进制,不带0x。integer为整型,'x'可换为'o','b','d'相对应八、二、十进制。

    int(string, number) 将任意进制的s(string类型)转换为十进制。s与number的进制类型需匹配,如s是16进制,则number=16,否侧会出错。若s为16进制,0x可带可不带,其他进制同。


实例:

a=10086
s=0xdeadbeef
ss='0xdeadbeef'
a_hex = hex(a)                 ===>        0x2766
a_hex = hex(a)[2:]            ===>        2766 
a_oct = oct(a)                  ===>        0o23546
a_bin = bin(a)                  ===>        0b10011101100110
s_dec = format(s,'d')       ===>        3735928559

s_int = int(ss,16)              ===>        3735928559


代码演示:

# 十进制转其他:

number1 = 13581887557
# 十进制整数转换为十六进制
hex_number1_0x = hex(number1)
hex_number1 = hex(number1)[2:]
hex_number2 = format(number1, 'x')
print("Decimal integer %s converts to hex with 0x via 'hex' : %s" % (number1, hex_number1_0x))
print("Decimal integer %s converts to hex without 0x via 'hex' : %s" % (number1, hex_number1))
print("Decimal integer %s converts to hex without 0x via 'format' : %s" % (number1, hex_number2))
# 十进制整数转换为八进制 0o123456形式
oct_number1_0o = oct(number1)
oct_number1 = oct(number1)[2:]
oct_number2 = format(number1, 'o')
print("Decimal integer %s converts to octal with 0o via 'oct' : %s" % (number1, oct_number1_0o))
print("Decimal integer %s converts to octal without 0o via 'oct' : %s" % (number1, oct_number1))
print("Decimal integer %s converts to octal without 0o via 'format' : %s" % (number1, oct_number2))
# 十进制整数转换为二进制 0b123456形式
bin_number1_0b = bin(number1)
bin_number1 = bin(number1)[2:]
bin_number2 = format(number1, 'b')
print("Decimal integer %s converts to binary with 0b via 'bin' : %s" % (number1, bin_number1_0b))
print("Decimal integer %s converts to binary without 0b via 'bin' : %s " % (number1, bin_number1))
print("Decimal integer %s converts to binary without 0b via 'format' : %s " % (number1, bin_number2))

测试打印结果:

Decimal integer 13581887557 converts to hex with 0x via 'hex' : 0x3298b2845
Decimal integer 13581887557 converts to hex without 0x via 'hex' : 3298b2845
Decimal integer 13581887557 converts to hex without 0x via 'format' : 3298b2845
Decimal integer 13581887557 converts to Octal with 0o via 'oct' : 0o145142624105
Decimal integer 13581887557 converts to Octal without 0o via 'oct' : 145142624105
Decimal integer 13581887557 converts to Octal without 0o via 'format' : 145142624105
Decimal integer 13581887557 converts to binary with 0b via 'bin' : 0b1100101001100010110010100001000101
Decimal integer 13581887557 converts to binary without 0b via 'bin' : 1100101001100010110010100001000101
Decimal integer 13581887557 converts to binary without 0b via 'format' : 1100101001100010110010100001000101


# 十六进制转其他:

hex_int = 0x3298B2845
hex_str_0x = '0x3298B2845'
hex_str = '3298B2845'
# 十六进制转换为十进制整数
# hex_to_dec1 = int(hex_int, 16) 不成立,因为int(s,number)中的s必须要是string类型
hex_to_dec1 = format(hex_int, 'd')
hex_to_dec2 = int(hex_str, 16)
hex_to_dec3 = int(hex_str_0x, 16)
print("Hex integer (with 0x) cannot directly converts to demcimal via 'int', it must be string.")
print("Hex integer (with 0x) %s converts to demcimal via 'format' : %s" % (hex_str, hex_to_dec1))
print("Hex string (with 0x) %s converts to demcimal via 'int' : %s" % (hex_str_0x, hex_to_dec2))
print("Hex string (without 0x) %s converts to demcimal via 'int' : %s" % (hex_str, hex_to_dec3))
# 十六进制转换为八进制整数
hex_to_otc1 = format(hex_int, 'o')
hex_to_otc2 = oct(hex_int)
hex_to_otc3 = oct(hex_int)[2:]
print("Hex integer (without 0x) %s converts to octal without 0o via 'format' : %s" % (hex_str, hex_to_otc1))
print("Hex integer (with 0x) %s converts to octal with 0o via 'oct' : %s" % (hex_str_0x, hex_to_otc2))
print("Hex integer (without 0x) %s converts to octal without 0o via 'oct' : %s" % (hex_str, hex_to_otc3))
# 十六进制转换为二进制整数
hex_to_bin1 = format(hex_int, 'b')
hex_to_bin2 = bin(hex_int)
hex_to_bin3 = bin(hex_int)[2:]
print("Hex integer (without 0x) %s converts to binary without 0b via 'format' : %s" % (hex_str, hex_to_bin1))
print("Hex integer (with 0x) %s converts to binary with 0b via 'bin' : %s" % (hex_str_0x, hex_to_bin2))
print("Hex integer (without 0x) %s converts to binary without 0b via 'bin' : %s" % (hex_str, hex_to_bin3))

# 测试打印结果:

Hex integer (with 0x) cannot directly converts to demcimal via 'int', it must be string.
Hex integer (with 0x) 3298B2845 converts to demcimal via 'format' : 13581887557
Hex string (with 0x) 0x3298B2845 converts to demcimal via 'int' : 13581887557
Hex string (without 0x) 3298B2845 converts to demcimal via 'int' : 13581887557
Hex integer (without 0x) 3298B2845 converts to octal without 0o via 'format' : 145142624105
Hex integer (with 0x) 0x3298B2845 converts to octal with 0o via 'oct' : 0o145142624105
Hex integer (without 0x) 3298B2845 converts to octal without 0o via 'oct' : 145142624105
Hex integer (without 0x) 3298B2845 converts to binary without 0b via 'format' : 1100101001100010110010100001000101
Hex integer (with 0x) 0x3298B2845 converts to binary with 0b via 'bin' : 0b1100101001100010110010100001000101
Hex integer (without 0x) 3298B2845 converts to binary without 0b via 'bin' : 1100101001100010110010100001000101


# 二进制转其他:

bin_int = 1100101001100010110010100001000101
bin_str_0x = '0b1100101001100010110010100001000101'
bin_str = '1100101001100010110010100001000101'
# 二进制转换为十进制整数
# hex_to_dec1 = format(hex_int, 'd') 这个转过来还是1100...,可能因为format识别不了它是二进制的吧。在python中加只有十六进制的加0x能识别,二进制的0b就不行。
bin_to_dec2 = int(bin_str, 2)
bin_to_dec3 = int(bin_str_0x, 2)
print("Binary string (with 0o) %s converts to demcimal via 'int' : %s" % (bin_str_0x, bin_to_dec2))
print("Binary string (without 0o) %s converts to demcimal via 'int' : %s" % (bin_str, bin_to_dec3))
# 二六进制转换为八进制整数
bin_to_otc1 = format(bin_int, 'o')
bin_to_otc2 = oct(bin_int)
bin_to_otc3 = oct(bin_int)[2:]
print("Binary integer (without 0b) %s converts to octal without 0o via 'format' : %s" % (bin_str, bin_to_otc1))
print("Binary integer (with 0b) %s converts to octal with 0o via 'oct' : %s" % (bin_str_0x, bin_to_otc2))
print("Binary integer (without 0b) %s converts to octal without 0ovia 'oct' : %s" % (bin_str, bin_to_otc3))
# 二进制整数转换为十六进制
bin_to_hex1 = format(bin_int, 'x')
bin_to_hex2 = hex(bin_int)
bin_to_hex3 = hex(bin_int)[2:]
print("Binary integer (without 0b) %s converts to hex without 0x via 'format' : %s" % (bin_str, bin_to_hex1))
print("Binary integer (without 0b) %s converts to hex with 0x via 'hex' : %s" % (bin_str_0x, bin_to_hex2))
print("Binary integer (without 0b) %s converts to hex without 0x via 'hex' : %s" % (bin_str, bin_to_hex3))

# 测试打印结果:

Binary string (with 0o) 0b1100101001100010110010100001000101 converts to demcimal via 'int' : 13581887557
Binary string (without 0o) 1100101001100010110010100001000101 converts to demcimal via 'int' : 13581887557
Binary integer (without 0b) 1100101001100010110010100001000101 converts to octal without 0o via 'format' : 3307516405765622732254226501552745245
Binary integer (with 0b) 0b1100101001100010110010100001000101 converts to octal with 0o via 'oct' : 0o3307516405765622732254226501552745245
Binary integer (without 0b) 1100101001100010110010100001000101 converts to octal without 0ovia 'oct' : 3307516405765622732254226501552745245
Binary integer (without 0b) 1100101001100010110010100001000101 converts to hex without 0x via 'format' : 363d3a0bf5c97695896a0dabcaa5
Binary integer (without 0b) 0b1100101001100010110010100001000101 converts to hex with 0x via 'hex' : 0x363d3a0bf5c97695896a0dabcaa5
Binary integer (without 0b) 1100101001100010110010100001000101 converts to hex without 0x via 'hex' : 363d3a0bf5c97695896a0dabcaa5

八进制的懒得写了,一个用的少,再一个原理