Re: packing a C structure



haairam@xxxxxxxxx wrote:
hi ,,,

iam trying to pack a C structure to send via socket..i would like to
know how to frame the template for the C structure..

this is my 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;

kindly suggest me the format for the pack function call..this will
help me to solve my issue...


Transferring binary structures is inherently non-portable, so there is not a single way to deduce the actual binary layout of this structure, as the alignment and even the format of the binary numbers (e.g. endianness) will vary from machine type to machine type and even between compilers. It might even be impossible to transfer the data if the receiving machine is unable to handle 32 bit data, but I guess that's less of a problem.
That's why techniques like ASN.1 were developed.

If the amount of data is relatively small and you can live with some time spent in conversion, a simple (and more portable (*)) means to transfer this data is to transform it into ascii, with suitable separators, e.g. commas or colons, terminated by a line feed.

(*) Sheesh, I sure hope Scott Nudds doesn't read this ;-) It's "more" portable because you have removed alignment and endianness problems, there's still a portability issue in that the receiving machine might speak EBCDIC while the sending machine speaks ASCII.
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

.



Relevant Pages

  • Re: packing a C structure
    ... On Oct 26, 12:06 pm, Josef Moellers <josef.moell...@fujitsu- ... char b; ... there's still a portability issue in that the receiving machine might ... speak EBCDIC while the sending machine speaks ASCII. ...
    (comp.lang.perl.misc)
  • Re: Help - trying to serialize a struct
    ... I can convert the struct to a char* (string ... server and he must be able to deserialize the information. ... for widespread portability you must use the "b" flag when ... preserve the "alignment" of later fields of the structure. ...
    (comp.lang.c)
  • Re: Array of strings
    ... > pointer which is not guaranteed by the standard. ... > Recommending non-portable code without clearly stating it is ... >>> The code is portable if used with a char type. ... you see that I made a full disclosure on the code's portability ...
    (comp.lang.c)
  • Re: packing a C structure
    ... char b; ... endianness) will vary from machine type to machine type and even between ... there's still a portability issue in that the receiving machine might ... if you have the socket open, e.g. in $sock, just do ...
    (comp.lang.perl.misc)
  • Re: packing a C structure
    ... char b; ... can u provide a guideline for the format in the Pack command that i ... memory, only how the language access it. ... vs little endian memory layout. ...
    (perl.beginners)