Home > Blockchain >  How to encode and decode string to like "\x70\x61\x75\x6c" this in python [closed]
How to encode and decode string to like "\x70\x61\x75\x6c" this in python [closed]

Time:10-04

i am having some keys to send to a server and wanted it encoded.The best was to do it is make the string like ("\x70 \x61 \x75 \x6c") and it and decode it in server.But here i donot know to to convert p a u l (decoded from ("\x70 \x61 \x75 \x6c")) this to string.

i want to do Like :- paul encrypt to ("\x70 \x61 \x75 \x6c") and ("\x70 \x61 \x75 \x6c") decrypt to paul.Thats all.

CodePudding user response:

To encode p a u l into ("\x70 \x61 \x75 \x6c"), you could use:

s = 'p a u l'
encoded = '("%s")' % ''.join(['\\x' c.encode('utf-8').hex() if c != ' ' else c for c in s])

to decode:

encoded = r'("\x70 \x61 \x75 \x6c")'
' '.join(bytearray.fromhex(x[2:]).decode() for x in encoded[2:-2].split(' '))
  • Related