Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > hex and little endian

Reply
Thread Tools

hex and little endian

 
 
Matteo Memelli
Guest
Posts: n/a
 
      12-13-2003
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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-13-2003
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>




 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-13-2003
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/li...le-struct.html for
details.

Peter

 
Reply With Quote
 
Matteo Memelli
Guest
Posts: n/a
 
      12-13-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big-endian, little-endian and sizeof() in different systems Javier C++ 6 06-16-2007 12:43 PM
Big Endian and Little Endian bhatia C++ 2 07-07-2006 06:48 PM
What's the memory layout of bit field struct in little-endian and big-endian platform? aling C++ 8 10-19-2005 04:50 PM
How to eliminate the bitmap difference in little endian and big endian? Eric J.Hu C++ 7 09-07-2005 10:27 AM
How to eliminate the bitmap difference in big endian and little endian? Eric J.Hu C Programming 3 08-29-2005 09:22 PM



Advertisments