simple way of converting ASCII into HEX with no change in value

tryanderror :

What is the easiest way of converting a ASCII-String into hex without changing the value?

what i have:

string = "00FA0086"

what i want:

hex_string = b'\x00\xFA\x00\x86"

Is there a simple way or do i need to write a function?

Serge Ballesta :

You are looking for the binascii module from the Standard Python Library:

import binascii

string = "00FA0086"

print(repr(binascii.a2b_hex(string)))

gives:

b'\x00\xfa\x00\x86'

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4010&siteId=1