我正在尝试编写一种将十六进制单词转换为有符号整数的方法。我想使用python 2-7。在python3中,我可以执行以下操作
def word2int(hex_str):
ba = bytes.fromhex(hex_str)
return int.from_bytes(ba,byteorder='big',signed=True)但是,在python2-7中没有定义这些方法(例如,fromhex十六进制和from_bytes)。在Python2-7中有什么好的、简单的方法来做到这一点吗?
发布于 2014-06-26 12:13:01
使用int可转换为无符号整数,然后手动转换为有符号整数。
def word_to_int(hex_str):
value = int(hex_str, 16)
if value > 127:
value = value-256
return valuehttps://stackoverflow.com/questions/24428059
复制相似问题