我正在尝试解码Python ()函数的结果。根据Python文档,这将返回:
包含其pid和退出状态指示的元组:16位数字,其低字节是终止进程的信号号,其高字节是退出状态(如果信号号为零);如果生成核心文件,则设置低字节的高位。
如何解码退出状态指示(这是一个整数)以获得高字节和低字节?具体而言,如何实现以下代码段中使用的解码函数:
(pid,status) = os.wait()
(exitstatus, signum) = decode(status) 发布于 2008-08-13 17:56:34
这能做你想做的事:
signum = status & 0xff
exitstatus = (status & 0xff00) >> 8发布于 2008-08-13 18:52:36
要回答您的一般问题,可以使用位操纵
pid, status = os.wait()
exitstatus, signum = status & 0xFF, (status & 0xFF00) >> 8但是,也有用于解释退出状态值的内建函数:
pid, status = os.wait()
exitstatus, signum = os.WEXITSTATUS( status ), os.WTERMSIG( status )另请参阅:
发布于 2009-03-26 14:52:42
您可以使用结构模块将int分解为无符号字节的字符串:
import struct
i = 3235830701 # 0xC0DEDBAD
s = struct.pack(">L", i) # ">" = Big-endian, "<" = Little-endian
print s # '\xc0\xde\xdb\xad'
print s[0] # '\xc0'
print ord(s[0]) # 192 (which is 0xC0)如果您将其与数组模块结合起来,您可以更方便地这样做:
import struct
i = 3235830701 # 0xC0DEDBAD
s = struct.pack(">L", i) # ">" = Big-endian, "<" = Little-endian
import array
a = array.array("B") # B: Unsigned bytes
a.fromstring(s)
print a # array('B', [192, 222, 219, 173])https://stackoverflow.com/questions/10123
复制相似问题