Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I want to convert a hexadecimal string like 1030 to a byte array like b'\x10\x30'
1030
b'\x10\x30'
I know we can use bytearray.fromhex("1030") or "1030".decode("hex"). However, I get output '\x100'.
bytearray.fromhex("1030")
"1030".decode("hex")
'\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).
bytearray(b'\x100')
\x10
0
\x30
Add a comment
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.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.