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 each element of a list to a string for processing tester_V 6 337 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 584 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 651 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  filecmp is not working for UTF-8 BOM encoded files sureshnagarajan 3 470 Feb-10-2021, 11:17 AM
Last Post: sureshnagarajan
  Convert string to JSON using a for loop PG_Breizh 3 552 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  Convert hex value into string Sancho_Pansa 8 818 Dec-23-2020, 08:54 AM
Last Post: Sancho_Pansa
  Error with non-ASCII character? giomach 10 1,156 Nov-09-2020, 01:13 PM
Last Post: giomach
  convert string into multiple columns in python3 VAN 2 766 Sep-26-2020, 11:14 PM
Last Post: scidam
  get original code after being encoded to UTF-8 ashok 18 1,721 Sep-08-2020, 04:17 AM
Last Post: ndc85430
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 945 Aug-08-2020, 10:44 PM
Last Post: mandaxyz

Forum Jump:

User Panel Messages

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