Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert STDOUT hex output to string in Python

Tags:

python

string

hex

I've looked for ways to convert the following type of hex output to string characters in Python. The output comes from STDOUT earlier in my code and I want to be able to process it further.

Let me give you some examples of the string I want converted:

\x5f\x63\x6c\x6f\x73\x65\x28

I want this type of string to be stripped of '\x' and I am only interested in the two hex characters that follow. Preferably, I would like to convert strings like this into;

5f 63 6c 6f 73 65 28

The original input is interpreted as a string, but I am not able to simply strip the \x in Python itself. It would be great if I can solve this conversion internally within Python?

like image 952
Marek Avatar asked Mar 17 '26 01:03

Marek


1 Answers

>>> '\x12\x34\x56\x78'.encode('hex')
'12345678'
like image 171
Ignacio Vazquez-Abrams Avatar answered Mar 18 '26 15:03

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!