How to convert a hexadecimal string to a byte array in Python

Converting a hexadecimal string to a byte array translates the hexadecimal string to an array of bytes. For example, converting “AB” to a byte array results in bytearray(b’\xab’).

Solution for How to convert a hexadecimal string to a byte array in Python : You can use bytearray.fromhex() to convert a hexadecimal string to a byte array Call bytearray.fromhex(string) with a hexadecimal string as string to convert it to a byte array. The hexadecimal string must contain two hexadecimal digits per byte and must not begin with “0x”.


how-to-convert-a-hexadecimal-string-to-a-byte-array-in-python