Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

Re: Converting a C structure to Perl

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://50023]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.