hex and little endian

M

Matteo Memelli

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:
16

Can anyone help me?
Thank you very much in advance

Matte
 
F

Fredrik Lundh

Matteo said:
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?
'\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>
 
P

Peter Otten

Matteo said:
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:
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():

However, the cleaner approach is:

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
 
M

Matteo Memelli

Il Sat, 13 Dec 2003 13:13:24 +0100, Fredrik Lundh ha scritto:
'\xf1\xe2\xff\xbf'

(see the library reference for details)


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

</F>

Thanks you guys :)
Matte
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top