python 将负整数转换为字节(singed=True),但转换回来,它变成正数

Ath*_*hos 3 python signed byte

number = -127
array = number.to_bytes( 1 , byteorder='big' , signed=True )
Run Code Online (Sandbox Code Playgroud)

只转换为单个字节

print( array[0] )

number_positive = 254
array = array + number_positive.to_bytes( 1 , byteorder='big' , signed=False )
Run Code Online (Sandbox Code Playgroud)

那我应该如何分别打印-127和254呢

如果我只使用array[0] and array[1]答案将是两个积极的

非常感谢您的帮助 提前谢谢您

blh*_*ing 5

您应该使用该int.from_bytes()方法将字节转换回整数:

print(int.from_bytes(array, byteorder='big', signed=True))
Run Code Online (Sandbox Code Playgroud)