I am defining a byte array in python and the same value shows up multiple times and I would like to replace that value by a variable. Here's an example of a similar code:
btarray = bytearray([
0xa9, 0x01,
0x6d, 0x01
])
I want to, instead of writing 0x01 all the time, just write x, might the following be working?
x = 0x01
btarray = bytearray([
0xa9, x,
0x6d, x
])
Is it possible?
I finally understood your question! The answer is yes , Unfortunately I don't have any documentation to link to. since this is quite basic and low-level.
Yes you can, Just like any other array. Bytearray is iterable: So you could do something like this:
x = someByteValue
for n,i in enumerate(btarray):
if i == 0x01:
btarray[n] = x
Another way would be to use a simple for loop:
x = someByteValue
for i in range(len(btarray)):
if btarray[i] == 0x01:
btarray[i] = x
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.