0

I have the following where I need s1 to be in hexes, but not necessarily printable characters. I use join to make them like \x42\x42\x42\x42.

s1 = '42424242'
s2 = "\\x".join(s1[i:i + 2] for i in range(len(s1), -2, -2))
s3 = "AAAA"
print(s3 + s2)

When I print it out, it currently prints as AAAA\x42\x42\x42\x42, but I need it to be AAAABBBB. Anyone help? edit: I am not able to use binascii for this case.

Ian Low
  • 399
  • 4
  • 14
  • What you actually produced was `r'AAAA\x42\x42\x42\x42'` or `'AAAA\\x42\\x42\\x42\\x42'`, and you needed `'AAAA\x42\x42\x42\x42'` (the `'\\'` remained as a literal backslash character, rather than being interpreted to be the ascii escape sequence). If you insist on this approach, you will need to turn `s2` into `bytes` and then call `.decode('unicode_escape')`, i.e. `b'\\x42\\x42\\x42\\x42'.decode('unicode_escape')`. – metatoaster Mar 30 '23 at 00:32

0 Answers0