hex value in string back to real hex value

Phil Frost indigo at bitglue.com
Tue Sep 7 13:37:00 EDT 2004


I'm not sure just what your problem is here. The best solution would be
to make your strings "\xe7" and not "\\xe7". The former contains the
real byte 0xe7, while the latter contains three bytes. When the former
is written to a file, you will get whatever letter 0xe7 represents
depending on what encoding you use.

If you have a reason to not do that (perhaps you are prompting for some
data and want escapes to be recgonized) then you could use something
like this:

a = sys.stdin.readline()
a = eval('"""'+a.replace('"""','"""+\'"""\'+"""')+'"""')

On Tue, Sep 07, 2004 at 09:47:01AM -0700, jack wrote:
> Hi everyone,
> 
> I get in a program an hexa value codes in a string for example \xe7
> which correspond to a french character.
> 
> The string would be "\xe7t\xe7" and I want to write in a file ?t? and
> not \xe7t\xe7.
> 
> Sure I could write a function to analyse the string, find all the hexa
> caracter (because of the \x) and convert the value to decimal and back
> to the correct cahracter.
> 
> Is there something simplier ?
> 
> An example below for generating such a string :
> a=""
> a=a+"\\"
> a=a+"x"
> a=a+"e"
> a=a+"7"
> len(a)= 4 # so it is not understood as the hexa value !!
> a
> "\xe7"
> 
> 
> Thanks in advance.
> 
> Jack.



More information about the Python-list mailing list