python cookbook 3.4 二八十六进制整数,二进制八进制十六进制十进制准换

#需要转换或者输出使用二进制,八进制或者十六进制表示的整数。可以分别使用bin(),oct(),hex()函数
x=1234
print(bin(x))
print(oct(x))
print(hex(x))

#整数有符号,若处理负数,输出结果会包含一个负号。
x=-1234
print(format(x,'b'))
print(format(x,'x'))

#若想产生一个无符号值,需要增加一个指示最大位长度的值。比如为了显示32位值,可以这样写:
x=-1234
print(format(2**32+x,'b'))
print(format(2**32+x,'x'))

#为了以不同进制转换整数字符串,简单的使用带有进制的int(0函数即可
print(int('4d2',16))
print(int('10011010010',2))

如要指定8进制,注意前缀是0o

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/89332922
今日推荐