Re: packing a C structure



On Oct 26, 10:37 pm, "jl_p...@xxxxxxxxxxx" <jl_p...@xxxxxxxxxxx>
wrote:
On Oct 25, 10:57 pm, haai...@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...

Hi,

I have the impression that what you want is to pack values in Perl
into a binary string of data that matches how a C program would do it.

If I'm right, this might work:

my $stringToSend = pack('I c16 I I c6 c8',
<YOUR PERL VARIABLES GO HERE>);

However, it's quite likely that the C program packs the data in a
way so that they are properly byte-aligned. If that's the case, the
above pack string may not work, and you'd probably have better luck
with the following pack string that inserts padding in the proper
places:

my $stringToSend = pack('I c16 x![I] I I c6 c8 x![I]',
<YOUR PERL VARIABLES GO HERE>);

Out of curiosity, when you declare b as a char array of 16 elements
is that because you're sending over 16 separate char values, or are
you sending one null-terminated string that can be a maximum of 15
characters long?

If your intention is to use b[16] as a single string (holding a
maximum of 15 characters), you probably want to replace "c16" in the
above pack strings to "Z16" (you may also want to replace "c6" and
"c8" with "Z6" and "Z8"). That way, instead of sending over 33
separate variables (that is, three ints plus 30 characters), you'd be
sending over six separate variables (that is, three ints plus three
strings).

So if it's the case that you want to send over three ints and three
strings, you might use code like this:

my $a = 1;
my $b = "Title of book";
my $c = 3;
my $d = 4;
my $e = "March";
my $f = "Sunday";
my $stringToSend = pack('I Z16 x![I] I I Z6 Z8 x![I]',
$a, $b, $c, $d, $e, $f);

Unfortunately, there's no real way to know what the proper pack
string should be unless you analyze the binary structure that your C
program writes out (or if can find one, reads in). But it's likely
that one of pack strings I gave you (or a variant) will work.

I hope this helps. Perl's pack() function is very powerful and
useful when dealing with C structures written out to disk.

-- Jean-Luc- Hide quoted text -

- Show quoted text -


hi ,,

Thanks for ur help....when i used the format specified by you i
observed a strange behaviour..i have given $a value as 271 ans i
expect that the data while sending through socket would be 00 00 01 0f
that is hex value(271) 0x010F but what i saw as a output of my socket
is data as 32 37 31...since i tried with L option as well both are
giving the same result.how to send that value as a hex itself..

for a variable $b i need to send 16 characters iam wondering during
packing what will happen to the Null Character of the string.

kindly provide a feedback.
Regards
Rams

.



Relevant Pages

  • Re: Is this string input function safe?
    ... return a pointer to mallocated memory holding one input string, ... See my comment after your call to fgets. ... char* malloc_getstr ... before any characters are read, then the ...
    (comp.lang.c)
  • Re: RfD: XCHAR wordset (for UTF-8 and alike)
    ... extended to work with xchars, ... >replacing one char with another. ... before-cursor part, even for fixed-width characters. ... So, should string words ...
    (comp.lang.forth)
  • RE: Fixed Length
    ... match these formats, but rather pull the existing, normal, formats from the ... and run a function to build that information into a string that you ... length and the total lenght of all the fields has to be 100 characters. ... JobId (8 char) ...
    (microsoft.public.access.modulesdaovba)
  • Re: Is this code totaly a shit?
    ... | void UppStrg(char *Low, char *Upp, int cnt); ... whitespace-delimited string. ... You're also assuming that the representations of the characters ...
    (comp.lang.c)
  • Re: Sorry, newbie question about generating a random string
    ... string grows to a max of 10 characters. ... The real problem is that you are not terminating the string. ... string is an array of characters ending in a null character, ... char myChar; ...
    (comp.lang.c.moderated)