将固定宽度有符号整数的固定宽度十六进制字符串转换为Python中的整数

时间:2017-11-21 22:14:16

标签: python python-3.x

我有一个5位(20位)十六进制字符串,它以二进制补码编码有符号整数。是否有一种紧凑,简单的方法来在Python中提取整数?

1 个答案:

答案 0 :(得分:0)

n = int(s, 16)    # get the unsigned value of your hex string
if n >= 2**19:    # if sign bit is set...
    n -= 2**20    # ...make it negative
相关问题