1

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?

Mero
  • 1,280
  • 4
  • 15
  • 25

2 Answers2

1

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).

Błotosmętek
  • 12,717
  • 19
  • 29
0

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.

Mr. VK
  • 49
  • 4