\x to x with python

Question:

I have a string like this:

'\xac\x85'

and I want to encode these string like this another:

'xacx85'

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?

Asked By: XBoss

||

Answers:

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

>>> print ('\xac\x85')
xacx85
Answered By: Sunitha

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 'xacx85' in it" proving the '\x' to 'x' was successful!
#TIP: never trust UTF-8 for byte encoding!!
Answered By: BILLPC2684
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.