I have a Hex Array which looks like :
31 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
When converting this to binary this looks like : 0011000100110001
Each bit is a flag that relates to a number within the array. In this case this binary number would equal 2,3,7,10,11,15.
I’m not sure if there is a name for this notation, but is there any easy method to convert the hex about to get a list of decimal numbers as shown above.
So,
Each 0x31 equates to a byte or 8 bits.
Each 0x31 converts to 00110001.
The way that this binary is then supposed to be interpretted is.
0 1 2 3 4 5 6 7 8 9 10
0 0 1 1 0 0 0 1 ......
Here you can see I get the decimal values 2,3,7 from the 0x31.
Hope this makes sense. Any help would be greatly appreciated.
So we have the hex numbers in a space-separated string.
Now we split the string, convert each byte from hex string to int (
int('31', 16) == 49), then convert it to binary string (bin(49) == '0b110001'), then take away the'0b'with[2:], add zeroes at the beginning so the sequence exactly 8 long ('110001'.zfill(8) == '00110001'). Then we join all the bit strings together in one string.trans = {'0':'0000','1':'0001','2':'0010','3':'0011','4':'0100','5':'0101','6':'0110','7':'0111','8':'1000','9':'1001','a':'1010','b':'1011','c':'1100','d':'1101','e':'1110','f':'1111',' ':''} s = ''.join(trans[c] for c in s.lower())Then we
enumeratethe bits, so every bit (b) will have a corresponding position (i), just as you described. We use a list comprehension and include only those positions at which the symbol is'1'.