简体   繁体   English

将1个变量中的2字节十六进制分成2个

[英]Separate 2 Bytes Hexadecimal in 1 variable into 2

I have 我有

2bytes=0x1AFF

and I want to split the variable "2Bytes" to save 0x1A in one independent variable, and 0xFF in another independent variable. 我想拆分变量“ 2Bytes”以将0x1A保存在一个自变量中,将0xFF保存在另一个自变量中。 Saving in one variable the first byte and in the another the second: 在第一个字节中保存一个变量,在第二个字节中保存另一个变量:

Byte1=0x1A
Byte=0xFF

How can I do that ? 我怎样才能做到这一点 ? Thanks 谢谢

I would use the bitwise and & and bit shifting >> operators: 我将使用按位和&和和位移>>运算符:

value = 0x1AFF
values = []
while value != 0:
    values.append(value & 0xFF)
    value = value >> 8

print(values)
# [255, 26]
# ==
# [0xFF, 0x1A]

This should produce the bytes (in reverse order) of value as a list in the variable values . 这应该产生的字节(以相反的顺序) value作为变量的列表values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM