每天CookBook之Python-044
- 二进制、八进制和十六进制转换
x = 1234
print(bin(x))
print(oct(x))
print(hex(x))
print(format(x, 'b'))
print(format(x, 'o'))
print(format(x, 'x'))
x = -1234
print(format(x, 'b'))
print(format(x, 'x'))
print(format(2 ** 32 + x, 'b'))
print(format(2 ** 32 + x, 'x'))
print(int('4d2', 16))
print(int('10011010010', 2))
Out:
0b10011010010
0o2322
0x4d2
10011010010
2322
4d2
-10011010010
-4d2
11111111111111111111101100101110
fffffb2e
1234
1234