如何在Python中将二进制字符串转换为相应的十六进制值?
我有0000 0100 1000 1101,我想使用Python 2.6获得048D。
参考方案
int以2为底,然后是hex:
>>> int('010110', 2)
22
>>> hex(int('010110', 2))
'0x16'
>>>
>>> hex(int('0000010010001101', 2))
'0x48d'
int的文档:
int(x[, base]) -> integer
Convert a string or number to an integer, if possible. A floating
点
参数将被截断为零(不包含字符串)
表示浮点数!)转换字符串时,
用
可选的基础。转换基数时提供基数是错误的
非字符串。如果base为零,则根据
字符串内容。如果参数超出整数范围,则a
将返回long对象。
hex的文档:
hex(number) -> string
Return the hexadecimal representation of an integer or long
整数。
Python 3运算符>>打印到文件 - python
我有以下Python代码编写项目的依赖文件。它可以在Python 2.x上正常工作,但是在使用Python 3进行测