473,503 Members | 5,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hex and little endian

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

Jul 18 '05 #1
3 9462
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

Jul 18 '05 #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>


Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
28994
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
3
5176
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
27438
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
6527
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
3138
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
3
2606
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian...
6
3159
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
14150
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
2
4027
by: Ramesh | last post by:
Hi I have a structure as below on big endian based system typedef struct { unsigned long LedA:5; unsigned long LedB:4; unsigned long LedC:8;
23
7004
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.