[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Hex string to ascii string

Posted on 2009-02-08
9
Medium Priority
?
1,355 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
0
Comment
Question by:CSecurity
9 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 23587776
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).
0
 
LVL 29

Expert Comment

by:pepr
ID: 23587823
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... ?
0
 
LVL 17

Author Comment

by:CSecurity
ID: 23588434
yes
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:pepr
ID: 23588517
Well, what character should be AA ? (What encoding do you use?)
0
 
LVL 17

Expert Comment

by:ramrom
ID: 23595838
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

0
 
LVL 2

Accepted Solution

by:
jdevera earned 2000 total points
ID: 23595868
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

0
 
LVL 29

Expert Comment

by:pepr
ID: 23601670
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

0
 
LVL 2

Expert Comment

by:jdevera
ID: 23601906
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.
0
 
LVL 29

Expert Comment

by:pepr
ID: 23608907
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question