Python - 从十六进制整数转换为十六进制字符串

标签 python python-2.7

有没有一种相当简单的方法可以在 Python 中将十六进制整数转换为十六进制字符串? 例如,我有 0xa1b2c3,我想要 "0xa1b2c3"。如果我使用 str(),它会自动转换为基数 10,然后我无法将它转换回来,因为此时它是一个字符串。

最佳答案

为什么不直接做 hex()

>>> testNum = 0xa1b2c3
>>> hex(testNum)
    '0xa1b2c3'
>>> test = hex(testNum)
>>> isinstance(test, str)
    True

hex 返回一个字符串表示。请参阅帮助(十六进制)

hex(...)
    hex(number) -> string

    Return the hexadecimal representation of an integer or long integer.

关于Python - 从十六进制整数转换为十六进制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495920/

相关文章:

python - Pandas - 将所有列中的特定值替换为另一列中的相应值

python - Django:没有名为模型的模块

python - 无法更新多处理的 manager.dict() 中的嵌套字典值

python - Django - Celery 连接被拒绝

python - 使用map或split(pyspark)后如何查看RDD的内容?

Python:终止子进程=成功,但它仍在运行(?)

python - Airflow HiveOperator 不工作

mysql - 如何使用 MySQL python 连接器修复批量加载

python - 用ast重写代码; Python

python - OpenCV : Is there an alternative to cv2. inRange 使用半径?