python:整数在二进制、八进制、十进制和十六进制之间转换
本文使用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
代码演示:
# 十进制转其他:
测试打印结果:
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 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
# 二进制转其他:
# 测试打印结果:
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
八进制的懒得写了,一个用的少,再一个原理
上一篇: 格式化日期
推荐阅读
-
Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)
-
Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)
-
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
-
Python:将十进制数转换为二进制,八进制,十六进制数
-
栈实现十进制转换为二进制,八进制和十六进制
-
二进制、八进制、十进制、十六进制(整数、小数)之间的转换
-
python:整数在二进制、八进制、十进制和十六进制之间转换
-
python:整数在二进制、八进制、十进制和十六进制之间转换
-
递归函数实现十进制正整数转换为二进制,八进制,十六进制
-
十进制整数与二进制整数、十六进制整数之间的转换