I want to convert a decimal integer into a \xLO\xHI
hex string while keeping the "\x" prefix and without translating printable ASCII to their equivalent ASCII.
What I want to achieve:
>>> dec_to_hex(512)
"\x00\x02"
The following solutions I found while searching for an answer aren't good enough and I'll explain why:
This one didn't put the "\x" prefix and don't translated it to bytes
>>> hex(512)
'0x200'
This examples is really close but receives hexadecimal (I need decimal) and translated chars to ascii:
>>> from binascii import unhexlify
>>> unhexlify('65004100430005FF70000000')
'e\x00A\x00C\x00\x05\xffp\x00\x00\x00'
This one translates ASCIIinto chars:
>>> import struct
>>> struct.pack('<h', 512)
'\x00\x02'
>>> struct.pack('<h', 97)
'a\x00'