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] and array[1]
,答案将是两个肯定的
非常感谢任何帮助 预先谢谢你
答案 0 :(得分:2)
您应该使用int.from_bytes()
方法将字节转换回整数:
print(int.from_bytes(array, byteorder='big', signed=True))
答案 1 :(得分:0)
也许使用abs
:
print(abs(number))
或两者皆是:
print(-number)