0

How to transform this unicode hex string ?

u'\x95\x93\xdfn\xe3D\x18\xc5q\xebv\xb3\xb4\xbb\x80\xb4H+\x84P\x05W,$\xfe......'

To real this hex

'\x95\x93\xdf\x6e\xe3\x44\x18\xc5\x71\xeb...'
4
  • 2
    Possible duplicate of Python3 - How to convert a string to hex (see the answer from acw1668) Commented Apr 24, 2019 at 12:02
  • Have a look at this question here it might be able to give an idea Commented Apr 24, 2019 at 12:03
  • The unicode hex string isn't a string, but it contains hex values Commented Apr 24, 2019 at 12:08
  • "hex" is is a bit ambiguous. Do you mean "a string that completely consists of \x escape codes"? Can you also explain in one sentence what you are planning to use this for - partly because I don't know what this would be good for, but if nothing else then because people who are trying to do this same thing can find this thread more easily. Commented Apr 24, 2019 at 12:48

1 Answer 1

2

You could try

s = u'\x95\x93\xdfn\xe3D\x18\xc5q\xebv\xb3\xb4\xbb\x80\xb4H+\x84P\x05W,$'
s.encode('latin')

# b'\x95\x93\xdfn\xe3D\x18\xc5q\xebv\xb3\xb4\xbb\x80\xb4H+\x84P\x05W,$'                                 
Sign up to request clarification or add additional context in comments.

2 Comments

@snakecharmerb I'm not too deep into unicode, but afaik this is the same. The point here was to use the proper encoding, which in this case is Latin_1 (and e.g. not utf-8, cp1250, asciior whatever). And these encodings have several aliases, so latin works as well, and so does latin-1.
@snakecharmerb You're welcome - I learned it also today; see e.g. here: docs.python.org/2.4/lib/standard-encodings.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.