my_hexdata = "1a" scale = 16 # equal to hexadecimal bin(int(my_hexdata, scale))[2:].zfill(len(my_hexdata)*4) # By changing the parameter of the zfill function we allow for any length of # hexadecimal code, which is more useful when using this code.
Here is what the above code is Doing:
1. The int function takes the string my_hexdata and converts it to an integer
2. The bin function converts the integer to a binary string
3. The [2:] removes the first two characters of the string, which are ‘0b’
4. The zfill function adds zeros to the beginning of the string until it is
the length specified by the parameter.
5. The length of the string is calculated by multiplying the length of the
hexadecimal string by 4.