How to deal with negative numbers that returncode get from subprocess in Python?

我怕爱的太早我们不能终老 提交于 2019-12-01 00:47:17

Using NumPy: view the unsigned 32-bit int, 4294966989, as a signed 32-bit int:

In [39]: np.uint32(4294966989).view('int32')
Out[39]: -307

Using only the standard library:

>>> import struct
>>> struct.unpack('i', struct.pack('I', 4294966989))
(-307,)
jfs

To convert positive 32-bit integer to its two's complement negative value:

>>> 4294966989 - (1 << 32) # mod 2**32
-307

As @Harry Johnston said, Windows API functions such as GetExitCodeProcess() use unsigned 32-bit integers e.g., DWORD, UINT. But errorlevel in cmd.exe is 32-bit signed integer and therefore some exit codes (> 0x80000000) may be shown as negative numbers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!