-1

I have a string like this:

'\\xac\\x85'

and I want to encode these string like this another:

'\xac\x85'

I tried a lot of things like encode tu utf-8, replace \\x, etc. But when I print the result it always is with \\x.

Any idea?

XBoss
  • 119
  • 1
  • 10

2 Answers2

2

'\\xac\\x85' is the literal representation of '\xac\x85'. So you dont have to do any translation

>>> print ('\\xac\\x85')
\xac\x85
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • Nitpick: not internal representation, literal representation. Internal representation is something like the bytes corresponding to `005c 0078 0061 0063 005c 0078 0038 0035` or even `0000005c 00000078 00000061 00000063 0000005c 00000078 00000038 00000035`. Literal representation is how strings are written to be understandable to the compiler in code. – Amadan Jul 23 '18 at 09:21
0

I made this function when I had the same issue
I included a little demo! Hope this helps people!

def FixSlash(string):
 strarr = string.split("\\x");
 string = strarr[0]
 for i in strarr[1:]:
  string+=chr(int(i[:2],16))+''.join(i[2:])
 return string```

my_string = FixSlash("my string has '\\xac\\x85' in it")
print(my_string)                  # returns "my string has '¬' in it" \x85 acts like a new line
print(my_string.encode("latin1")) # returns b"my string has '\xac\x85' in it" proving the '\\x' to '\x' was successful!
#TIP: never trust UTF-8 for byte encoding!!