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...'
Slake
  • 2,080
  • 3
  • 25
  • 32
  • 2
    Possible duplicate of [Python3 - How to convert a string to hex](https://stackoverflow.com/questions/38909543/python3-how-to-convert-a-string-to-hex) (see the answer from acw1668) – Nick is tired Apr 24 '19 at 12:02
  • Have a look at this [question here](https://stackoverflow.com/questions/8088375/how-do-i-convert-a-single-character-into-its-hex-ascii-value-in-python) it might be able to give an idea – Ivan of uganda Apr 24 '19 at 12:03
  • The unicode hex string isn't a string, but it contains hex values – Slake Apr 24 '19 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. – Tomalak Apr 24 '19 at 12:48

1 Answers1

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,$'                                 
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • @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`, `ascii`or whatever). And these encodings have several aliases, so `latin` works as well, and so does `latin-1`. – SpghttCd Apr 26 '19 at 08:10
  • @snakecharmerb You're welcome - I learned it also today; see e.g. here: https://docs.python.org/2.4/lib/standard-encodings.html – SpghttCd Apr 26 '19 at 11:10