hex value in string back to real hex value

Discussion in 'Python' started by jack, Sep 7, 2004.

  1. jack

    jack Guest

    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.
     
    jack, Sep 7, 2004
    #1
    1. Advertising

  2. 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.


    You might want to take a look at the binascii module.

    --
    Vincent Wehren
     
    vincent wehren, Sep 7, 2004
    #2
    1. Advertising

  3. jack

    Phil Frost Guest

    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.
     
    Phil Frost, Sep 7, 2004
    #3
  4. jack

    Peter Otten Guest

    jack wrote:

    > 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.


    "\xe7t\xe7" _is_ the same as "été" if you are using e. g. the ISO-8859-1
    encoding, so chances are you will see été if you open the file in a text
    editor without the need of prior conversions.

    But I may be misunderstanding you and you really have "\\xe7t\\xe7" - in
    that case you can use de/encode to switch between the two representations:

    >>> "été".encode("string_escape")

    '\\xe9t\\xe9'
    >>> _.decode("string_escape")

    '\xe9t\xe9'
    >>> print _

    été

    (In the interpreter the result of the previous calculation is assigned to _
    if it is not None)

    Peter
     
    Peter Otten, Sep 7, 2004
    #4
  5. jack

    jack Guest

    Thanks,

    The solution provided by phil and peter works perfectly for my
    solution (and I will have to work a little more to understand them
    fully). I will check the binascii.

    Regards.
    Jack.

    Phil and peter

    Peter Otten <> wrote in message news:<chks4h$o8r$01$-online.com>...
    > jack wrote:
    >
    > > 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.

    >
    > "\xe7t\xe7" _is_ the same as "été" if you are using e. g. the ISO-8859-1
    > encoding, so chances are you will see été if you open the file in a text
    > editor without the need of prior conversions.
    >
    > But I may be misunderstanding you and you really have "\\xe7t\\xe7" - in
    > that case you can use de/encode to switch between the two representations:
    >
    > >>> "été".encode("string_escape")

    > '\\xe9t\\xe9'
    > >>> _.decode("string_escape")

    > '\xe9t\xe9'
    > >>> print _

    > été
    >
    > (In the interpreter the result of the previous calculation is assigned to _
    > if it is not None)
    >
    > Peter
     
    jack, Sep 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,407
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    535
    Juha Autero
    Aug 19, 2003
  3. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,140
  4. tim
    Replies:
    2
    Views:
    1,595
    Dennis Lee Bieber
    Nov 23, 2005
  5. Matthias Güntert
    Replies:
    1
    Views:
    390
    Simon Forman
    Aug 20, 2009
Loading...

Share This Page