Reputation: 383
In python, I'm trying to convert a string literal to it's hexadecimal 4 byte equivalent. Here's an example of what I'm doing:
import struct
struct.pack("<i", int("08050030", 16))
>>'0\x00\x05\x08'
Why is the output rendering like this? I would expect \x30\x00\x05\x08
?
Upvotes: 1
Views: 1079
Reputation: 304403
You would probably be surprised if the REPL did this
>>> "hello"
'\x68\x65\x6c\x6c\x6f'
Luckily it doesn't. printable characters are printed as themselves. unprintable characters use shortcuts such as '\n'
and when none is available, the last resort is to use the hex notation.
It's perfectly acceptable to use hex encoding anywhere in your literals
>>> '\x30\x00\x05\x08'
'0\x00\x05\x08'
It's just not Python's preference to use them for display.
Upvotes: 2