Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Managing C structs -with Perl-

by khkramer (Scribe)
on Jan 02, 2002 at 02:26 UTC ( [id://135567]=note: print w/replies, xml ) Need Help??


in reply to Managing C structs -with Perl-

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://135567]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found