bytearray

num = 0x7d00
a = num & 0xff			# 低位
b = (num >> 8) & 0xff	# 高位
c = num % 256			# 低位
d = num / 256			# 高位
print(a, b, c, d, type(a), type(b), type(c), type(d))
num_arr = bytearray([a, b])

在这2种取高低位的代码中,d是float的,这与C语言是不同的,在C中,d可以是unsigned char 类型,它最后的结果就是0x7d,但python不一样,它是125.0,因此在进行16进制格式通信时,应该使用第1种方式。

猜你喜欢

转载自blog.csdn.net/cp_srd/article/details/105225305