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

admin 2019-3-20 299

本文利用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'绝对应8、2、十进造。     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) 二十五味珍珠丸 http://www.shenhouyy.com/%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:] prin十八味党参丸t("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 八进造的懒得写了,一个用的少,再一个道理
绿皮书获奖引争议
编辑:周涛
最新回复 (0)
返回
发新帖