hex and little endian

Discussion in 'Python' started by Matteo Memelli, Dec 13, 2003.

  1. Hello I'm getting in trouble....
    I'm trying to convert an hex address like 0xbfffe2f1 in little endian
    format \xf1\xe2\xff\xbf. Is there any funcion that can make this?
    Whe I try to code somethig like this:
    "\x%s\x%s\x%s\x%s" % (string1, string2, string3, string4)
    I obtain this error:
    ValueError: invalid \x escape

    and if I escape the charachter "\" making something like this:
    "\\x%s\\x%s\\x%s\\x%s" % (string1, string2, string3, string4)
    It works but the resulting string is not an hex representation, I mean:
    >>> len('\xaa\xaa\xaa\xaa')

    4

    >>> len('\\xaa\\xaa\\xaa\\xaa')

    16

    Can anyone help me?
    Thank you very much in advance

    Matte
    Matteo Memelli, Dec 13, 2003
    #1
    1. Advertising

  2. Matteo Memelli wrote:

    > I'm trying to convert an hex address like 0xbfffe2f1 in little endian
    > format \xf1\xe2\xff\xbf. Is there any funcion that can make this?


    >>> import struct
    >>> struct.pack("<i", 0xbfffe2f1)

    '\xf1\xe2\xff\xbf'

    (see the library reference for details)

    > Whe I try to code somethig like this:
    > "\x%s\x%s\x%s\x%s" % (string1, string2, string3, string4)
    > I obtain this error:
    > ValueError: invalid \x escape
    > and if I escape the charachter "\" making something like this:
    > "\\x%s\\x%s\\x%s\\x%s" % (string1, string2, string3, string4)
    > It works but the resulting string is not an hex representation


    "\x" is used in string literals; it has no special meaning when it's
    used in a string object.

    </F>
    Fredrik Lundh, Dec 13, 2003
    #2
    1. Advertising

  3. Matteo Memelli

    Peter Otten Guest

    Matteo Memelli wrote:

    > Hello I'm getting in trouble....
    > I'm trying to convert an hex address like 0xbfffe2f1 in little endian
    > format \xf1\xe2\xff\xbf. Is there any funcion that can make this?
    > Whe I try to code somethig like this:
    > "\x%s\x%s\x%s\x%s" % (string1, string2, string3, string4)
    > I obtain this error:
    > ValueError: invalid \x escape
    >
    > and if I escape the charachter "\" making something like this:
    > "\\x%s\\x%s\\x%s\\x%s" % (string1, string2, string3, string4)
    > It works but the resulting string is not an hex representation, I mean:
    >>>> len('\xaa\xaa\xaa\xaa')

    > 4
    >
    >>>> len('\\xaa\\xaa\\xaa\\xaa')

    > 16
    >
    > Can anyone help me?
    > Thank you very much in advance
    >
    > Matte


    Every replacement of "\xNN" in a string is done by the compiler, so one way
    would be to feed your string to the compiler via eval():

    >>> s = "\\x%s\\x%s" % ("AA", "BB")
    >>> s

    '\\xAA\\xBB'
    >>> eval("'" + s + "'")

    '\xaa\xbb'
    >>>


    However, the cleaner approach is:

    >>> import struct
    >>> n = 0x00aa00bb
    >>> struct.pack("i", n)

    '\xbb\x00\xaa\x00'
    >>> struct.pack(">i", n)

    '\x00\xaa\x00\xbb'
    >>> struct.pack("<i", n)

    '\xbb\x00\xaa\x00'
    >>>


    The > and < flags are used to explicitly specify the byte order. See the
    documentation http://www.python.org/doc/current/lib/module-struct.html for
    details.

    Peter
    Peter Otten, Dec 13, 2003
    #3
  4. Il Sat, 13 Dec 2003 13:13:24 +0100, Fredrik Lundh ha scritto:

    > Matteo Memelli wrote:
    >
    >> I'm trying to convert an hex address like 0xbfffe2f1 in little endian
    >> format \xf1\xe2\xff\xbf. Is there any funcion that can make this?

    >
    >>>> import struct
    >>>> struct.pack("<i", 0xbfffe2f1)

    > '\xf1\xe2\xff\xbf'
    >
    > (see the library reference for details)
    >
    >> Whe I try to code somethig like this:
    >> "\x%s\x%s\x%s\x%s" % (string1, string2, string3, string4)
    >> I obtain this error:
    >> ValueError: invalid \x escape
    >> and if I escape the charachter "\" making something like this:
    >> "\\x%s\\x%s\\x%s\\x%s" % (string1, string2, string3, string4)
    >> It works but the resulting string is not an hex representation

    >
    > "\x" is used in string literals; it has no special meaning when it's
    > used in a string object.
    >
    > </F>


    Thanks you guys :)
    Matte
    Matteo Memelli, Dec 13, 2003
    #4
    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. Eric J.Hu
    Replies:
    3
    Views:
    819
    Alexei A. Frounze
    Aug 29, 2005
  2. Eric J.Hu
    Replies:
    7
    Views:
    517
    Jim Langston
    Sep 7, 2005
  3. aling
    Replies:
    8
    Views:
    941
    Pete Becker
    Oct 19, 2005
  4. bhatia

    Big Endian and Little Endian

    bhatia, Jul 7, 2006, in forum: C++
    Replies:
    2
    Views:
    469
    Robbie Hatley
    Jul 7, 2006
  5. Javier
    Replies:
    6
    Views:
    558
    Gennaro Prota
    Jun 16, 2007
Loading...

Share This Page