Re: packing a C structure
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Fri, 26 Oct 2007 11:54:39 -0400
On 10/26/07, haairam@xxxxxxxxx <haairam@xxxxxxxxx> wrote:
hi,,snip
iam new to perl..i would like to pack a C structure to send it via
socket....
this is my C structure
typedef struct buffer_t {
uint32_t a;
char b[16];
uint32_t c;
uint32_t d;
char e[6];
char f[8];
} buffer_t;
can u provide a guideline for the format in the Pack command that i
need to use...
Short answer:
You cant do this, find a better way to send the data (I would suggest
XML, YAML, or JSON).
Long answer:
You may be able to do this, but only for a given C compiler and OS
combination. ANSI C does not specify how structures are laid out in
memory, only how the language access it. Some architectures use all
sorts of padding to make accessing elements faster (this is why you
have to say sizeof(struct foo) to get the size of the structure
instead of just calculating it from the elements). You are also not
guaranteed what order the elements will be in. The elements
themselves may not even be of a given size since char, int, and long
only have minimum size definitions. Yet another hurdle is big endian
vs little endian memory layout. So, the upshot is you need to find
out exactly how the structure is laid out in memory. After you know
that, you can choose the proper pack string, but be prepared for it to
change. It is better to send the data in a platform neutral manner
and reconstitute it on the other end. XML is popular for some reason,
but there are many different data transport formats.
.
- References:
- packing a C structure
- From: haairam
- packing a C structure
- Prev by Date: Re: error in qx
- Next by Date: Finding time difference from two strings
- Previous by thread: Re: packing a C structure
- Next by thread: Separating the body of the text in email from its header
- Index(es):
Relevant Pages
|