We help IT Professionals succeed at work.

Hex string to ascii string

CSecurity
CSecurity asked
on
Medium Priority
1,407 Views
Last Modified: 2013-11-05
Hi

I have a string like this:

AAABACADAF0406A6A8A7A2A5

so a for loop needed to get 2 byte 2byte and convert each to ascii and store all in a variable.

I'm not so good in Python, so please advice.

Thanks from now
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Firstly, the values are not ASCII as the 8th bit is often set. What is the encoding?

There may be more methods to do that. The question is what is the motivation, what is the source of the data. You may be interested in low-level standard module binascii (http://docs.python.org/library/binascii.html#module-binascii) or in higher-level modules like base 64, binhex, uu (see the references at the bottom of the page).
CERTIFIED EXPERT

Commented:
Do I understand it well that you have a string 'AAABACADAF0406A6A8A7A2A5', then you want to get 'AA' substring, assuming it is a hex description of a byte you want to get decimal number (170) and convert it to the character in your encoding... ?

Author

Commented:
yes
CERTIFIED EXPERT

Commented:
Well, what character should be AA ? (What encoding do you use?)
ramromconsultant

Commented:
Here is an untested guess (assuming straight conversion to ASCII).
hexString = 'AAABACADAF0406A6A8A7A2A5'
result = ''
for x in range(0, len(hexString), 2):
  pair = hexString[x:x+2]
  numeric = int(pair, 16)
  ch = ord(numeric)
  result += ch

Open in new window

Commented:
Have a look at the code below, if I understood the question correctly, it does what you need.
import binascii
 
myhex="AAABACADAF0406A6A8A7A2A5"
mystring = ""
 
while(len(myhex) > 0):
    mystring += binascii.unhexlify(myhex[:2])
    myhex = myhex[2:]
 
print mystring # Or do whatever you want with it

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
Well, the ramrom's solution is the classical one (if no binascii module were here). The jdevera solution is overcomplicated. There is no need to cut the myhex to couples and join the results. See the snippet below.

Still, the question is whether the result is ASCII (it cannot be) and what it really is. The unhexlify() simply returns the binary result and cannot be reliably interpreted as ASCII or as a string at all. It will be more apparent in Python 3.0 where strings are always Unicode.
import binascii
src = 'AAABACADAF0406A6A8A7A2A5'
s = binascii.unhexlify(src)
print s

Open in new window

Commented:
That's actually very good pepr, much better than my solution; you should get the points.

In regards to the ASCII or not ASCII question, maybe all they wanted was to have the values in a string, or maybe they made this string up as an example.
CERTIFIED EXPERT

Commented:
jdevera: No problem here. No need to fiddle with the points ;)

For the ASCII/non-ASCII the problem is that the hex string may be encoded in some way, and trying to get the string this way may be wrong. This is my reason for pointing that out.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.