我需要一个函数,将二进制符号转换为整数.
int("1010",2)
#this will be converted to 10 instead of -6 -
# i want a fucnction that will convert it into -6
Run Code Online (Sandbox Code Playgroud)
您可以使用该bitstring模块:
from bitstring import Bits
nr = Bits(bin='1010')
print(nr.int) # .int here acts as signed integer. If you use .uint it will print 10
Run Code Online (Sandbox Code Playgroud)