I want to convert a hexadecimal string like 1030
to a byte array like b'\x10\x30'
I know we can use bytearray.fromhex("1030")
or "1030".decode("hex")
. However, I get output '\x100'
.
What am I missing here?
bytearray(b'\x100')
is correct, you just interpret it wrong way. It is character \x10
followed by character 0
(which happens to be ASCII for \x30
).
There is a built-in function in bytearray that does what you intend.
bytearray.fromhex("de ad be ef 00")
It returns a bytearray and it reads hex strings with or without space separator.