Convert hexadecimal characters to ascii

Albert Hopkins marduk at letterboxes.org
Sat Nov 29 16:29:21 EST 2008


On Sat, 2008-11-29 at 20:39 +0000, Durand wrote:
> Hi,
> 
> I've got this weird problem where in some strings, parts of the string
>  are in hexadecimal, or thats what I think they are. I'm not exactly
>  sure...I get something like this: 's\x08 \x08Test!' from parsing a log
>  file. From what I found on the internet, x08 is the backspace
>  character but I'm still not sure.

Yes, '\x08' is the ASCII code for backspace.  So if you truly wanted to
convert it to ASCII, as your state in your subject, then actually there
is nothing you need to do :-).

>  Anyway, I need to clean up this
>  string to get rid of any hexadecimal characters so that it just looks
>  like 'Test!'. Are there any functions to do this?

You will need to come up with the set of characters that are
(un)/acceptable to you and then strip those out.  I'm not sure what you
mean to just looks like 'Test!'.  If you removed all the backspaces it
would be 's Test!', but if you "played" the string then it would be
'Test!'.  If that's what you want then you need something that "acts
like a terminal emulator" on a string and gives you the resulting
output. The only I can think of that can probably do that for you is
pexpect[1].

1. http://www.noah.org/wiki/Pexpect





More information about the Python-list mailing list