【发布时间】:2021-01-26 03:52:58
【问题描述】:
我在将字符串(如输入)转换为 denary 时遇到了问题,因为字符串是使用十六进制所必需的(因为包含字符 a-f,它不能存储为整数)。以下是两种无效的尝试:
>>> hex_number = 'ff' #(255 in denary)
>>> ascii(0xhex_number)
SyntaxError: invalid hexadecimal literal
>>> ascii('0x'+hex_number)
"'0xff'"
我还看到您可以以某种方式使用format(),但这也不起作用。
>>> format(hex_number, '16') #This was what the demo said to do.
'ff '
我怎样才能将hex_number(十六进制)转换为十进制(又名十进制)或任何其他 n 基数系统?
【问题讨论】:
-
int('ff', 16)
标签: python python-3.x hex ascii