Skip to main content
 首页 » 编程设计

python之将16位的最高字节转换为python中的signed int

2025年05月04日10shihaiming

我使用 os.system 来运行 make 命令

os.system('make -C mydir/project all') 

我想看看 make 是否失败。系统文档指出返回代码的格式与 wait()

相同
Wait for completion of a child process, and return a tuple containing its pid  
and exit status indication: a 16-bit number, whose low byte is the signal number  
that killed the process, and whose high byte is the exit status (if the signal  
number is zero); the high bit of the low byte is set if a core file was produced. 

因此,如果 make(或其他应用程序)返回 -1,我必须将 0xFFxx(我并不关心被调用的 pid)转换为 -1。右移后,我得到 0xFF,但我无法将其转换为 -1,它始终打印 255。

那么,在 python 中,我如何将 255 转换为 -1,或者我如何告诉解释器我的 255 实际上是一个 8 位有符号整数?

请您参考如下方法:

if number > 127: 
  number -= 256