number = -127
array = number.to_bytes( 1 , byteorder='big' , signed=True )
只转换为单个字节
print( array[0] )
number_positive = 254
array = array + number_positive.to_bytes( 1 , byteorder='big' , signed=False )
那我应该怎么分别打印-127和254
如果我只使用 array[0] 和 array[1]
答案将是两个肯定的
非常感谢任何帮助 提前谢谢你
最佳答案
您应该使用 int.from_bytes()
方法将字节转换回整数:
print(int.from_bytes(array, byteorder='big', signed=True))
关于python 将负整数转换为字节(singed=True),但转换回来,它变成正数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022443/