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?

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

😁 thanks a lot. I was just confused
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.