【问题标题】:How to convert a hexadecimal string into a hexadecimal value? [duplicate]如何将十六进制字符串转换为十六进制值? [复制]
【发布时间】:2016-06-16 08:07:16
【问题描述】:

我一直在尝试在 Python 中将十六进制字符串转换为十六进制值,就像您可以将包含整数的字符串转换为整数值一样。我已经尝试了以下

>>> a = '0x25'
>>> b = hex(a)
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    b=hex(a)
TypeError: hex() argument can't be converted to hex

和

 >>> b = '20'
 >>> int(b)
 20

但我还没有找到任何可行的解决方案。

【问题讨论】:

    标签: python hex


    【解决方案1】:
    In [1]: int('0x20', 0)
    Out[1]: 32
    
    In [2]: int('20', 16)
    Out[2]: 32
    

    https://docs.python.org/3/library/functions.html#int

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 2019-07-27
      • 2012-02-03
      • 1970-01-01
      • 2017-08-23
      • 2014-03-19
      • 2018-01-31
      • 2015-02-06
      相关资源
      最近更新 更多