Skip to main content
 首页 » 编程设计

Python 字符串到整数值

2025年05月04日21pander-it

我想知道如何将 Python 中的字符串转换为相应的整数值,如下所示:

>>>打印 WhateverFunctionDoesThis('\x41\x42')

>>>16706

我四处搜索,但未能找到一种简单的方法来执行此操作。

谢谢。

请您参考如下方法:

>>> import struct 
>>> struct.unpack(">h",'\x41\x42') 
(16706,) 
>>> struct.unpack(">h",'\x41\x42')[0] 
16706 

对于其他格式字符,请参阅 the documentation