This is a perfect application for pack and unpack. It looks to me like your two transformation statements should be something like so:
my ($flag, $b1, $b2, $b3, $b4, $nextpass) = unpack ( "iiiiiZ*", $received_binary_struct ); my $binary_struct_to_send = pack ( "iZ*", $answer, $prevpass );
I wrote a little test-case using Inline::C to make sure that it looks like that to gcc, too <laugh>...
#!/usr/bin/perl -w use Inline C; use strict; $|++; my $recv = test_recv_struct(); print "recv: " . join ( ",", unpack ( "iiiiiZ*", $recv ) ) . "\n"; my $send = pack ( "iZ*", 1, "string two" ); test_send_struct ( $send ); __END__ __C__ struct recv { int flag; int b[4]; char nextpass[10]; }; typedef struct recv t_recv; struct send { int answer; char prevpass[10]; }; typedef struct send t_send; //----- SV* test_recv_struct() { t_recv* foo = malloc ( sizeof(t_recv) ); foo->flag = 1; foo->b[0] = 2; foo->b[1] = 3; foo->b[2] = 4; foo->b[3] = 5; sprintf( foo->nextpass, "a string" ); return newSVpv ( (char*)foo, sizeof(t_recv) ); } void test_send_struct ( char* perl_packed ) { t_send* foo = perl_packed; printf ( "send: %d,%s\n", foo->answer, foo->prevpass ); }

Inline is really nifty!

Kwin

In reply to Re: Managing C structs -with Perl- by khkramer
in thread Managing C structs -with Perl- by s0ttle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.