Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Converting a C structure to Perl

by Fastolfe (Vicar)
on Jan 05, 2001 at 12:10 UTC ( #50023=note: print w/ replies, xml ) Need Help??


in reply to Converting a C structure to Perl

Remember to close your <code> tags and your question won't be so difficult for us to read. :)

If you're looking to find the Perl equivalent of that data structure, another poster already nailed it for you: use a hash. If you're looking to pull in binary data created from that C structure into your Perl program, you probably want unpack. Something like this might do what you're looking for:

$_ = ...; # binary data in $_ my ($id, $secs, $version, $misc, $data) = unpack("a2iia10a9999", $_);
The format string (described in pack) basically means: 2 bytes of data, two integers, 10 bytes of raw data followed by 9999 bytes of raw data. Other things you might be able to take advantage of: Z for null-terminated strings, c for individual characters/bytes and n/N for "network" short/longs.

If you need to generate binary data with information you have, simply change unpack to pack and reverse your arguments around. Pass $id/$secs/etc and get back a binary representation. The format string remains the same.

When you say you want a 10,000 byte array, what are you talking about? Are you talking about a C array? I don't know how you expect to judge how many bytes a Perl array will consume. If you want 10,000 elements, that's already been given to you. 10,000 bytes in a string could be done by: "\0" x 10_000 Perhaps you could elaborate and/or use some more appropriate terminology so we can figure out what you mean? Heh.

Hope this helps.


Comment on Re: Converting a C structure to Perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-30 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls