Python Forum
convert hex encoded string to ASCII
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert hex encoded string to ASCII
#1
given a string of hexadecimal characters that represent ASCII characters, i want to convert it to those ASCII characters.  for example:

'707974686f6e2d666f72756d2e696f' -> 'python-forum.io'

in python 2 i can do .decode('hex') but this is gone in python3. so, i found this:
codecs.decode(codecs.decode('707974686f6e2d666f72756d2e696f','hex'),'ascii')
anything simpler that works in either python 2 or python 3 ?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
How about
codecs.decode("707974686f6e2d666f72756d2e696f", "hex")
Does it give the result you want?
Reply
#3
Personally, I've found the fromhex method of bytes to be useful.

print(bytes.fromhex('7370616d'))    # b'spam'
Reply
#4
(Sep-29-2016, 07:45 AM)j.crater Wrote: How about
codecs.decode("707974686f6e2d666f72756d2e696f", "hex")
Does it give the result you want?

not in py 3.  i want an ASCII or UTF-8 string, not an array of bytes. this why i added the 2nd call to codecs.decode(,"ascii").
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Quote:this why i added the 2nd call to codecs.decode(,"ascii")
You don't need a second call,just add .decode('utf-8').
>>> codecs.decode("707974686f6e2d666f72756d2e696f", "hex").decode('utf-8')
'python-forum.io'
It will work the same in Python 2.x
>>> codecs.decode("707974686f6e2d666f72756d2e696f", "hex").decode('utf-8')
u'python-forum.io'
Strings are Unicode by default in Python 3.x.
>>> u'python-forum.io' == 'python-forum.io'
True
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert result to hex ascii Jusufs 6 1,467 May-19-2023, 03:21 PM
Last Post: deanhystad
  convert string to float in list jacklee26 6 1,813 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,233 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question Can you put encoded strings into .txt files? Alivegamer 0 1,243 May-04-2022, 12:50 AM
Last Post: Alivegamer
Question How to convert string to variable? chatguy 5 2,226 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,394 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,272 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,166 Jun-16-2021, 02:11 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020