【问题标题】:invalid literal error when converting hex to int python [duplicate]将十六进制转换为int python时出现无效的文字错误[重复]
【发布时间】:2020-10-31 12:34:03
【问题描述】:

我正在尝试将此十六进制值转换为 int,但在 python 3.7 中出现无效的 litrel 错误

>>> int('1.222664064E9', 16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 16: '1.222664064E9'

【问题讨论】:

标签: python


【解决方案1】:

Use float.fromhex:

>>> float.fromhex('1.222664064E9')
1.1333982959172886
>>> int(_)
1

虽然你确定它是十六进制的吗?它看起来像一个指数符号的浮点数,其中E9 代表* 10 ** 9

>>> float('1.222664064E9')
1222664064.0
>>> int(_)
1222664064

【讨论】:

  • 我有 0.99E2% 的把握确定它是指数符号……也许交换答案的两个部分?
  • @tobias_k 我更喜欢它。 OP 说它是十六进制的,所以我相信他们的话,但提供我的保留。
  • @tobias_k 但现在你被证明是正确的,哈哈 :)
猜你喜欢
  • 2023-03-08
  • 2017-05-29
  • 1970-01-01
  • 2015-04-15
  • 2016-02-21
  • 2014-04-05
  • 2011-11-27
  • 1970-01-01
  • 2020-06-25
相关资源
最近更新 更多