perl pack() in C

Started by
1 comment, last by Arek the Absolute 16 years, 9 months ago
In order to interact with some net code written in Perl, certain information needs to be encoded using the pack() function. At the moment I'm not especially concerned with unpacking so much as packing data to send along. I'd like to write code in C to communicate with the Perl code, however, and it seems pointless to reinvent the wheel: I have trouble believing there's no C or C++ library available that implements the same functionality, but Googling has failed me and a friend of mine so far. My question is this: Is there any easy way to pack() data in a C program to be unpack()ed in Perl, or is it going to be necessary to write the functionality we're interested in ourselves? It's surprising me how hard it's been to identify some library that handles this.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Advertisement
Yes, of course. pack() was designed explicitly to deal with C, so there's no real library needed. Simply define a struct with the appropriate fields, cast a memory pointer to the struct type, and read/write the fields. Make sure to disable struct padding.
Thanks for the solution. Perl's still pretty alien to me at the moment so I tend to overthink it and assume there was something more complicated going on in the packing process.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig

This topic is closed to new replies.

Advertisement