Lars,

I believe 'unpack' is the right way to go, you just need to get the template 
right ('N' in your example)

See the explanation on http://www.perlmonks.org/?node_id=224666, especially the 
section on 'unpack'

"unpack takes a template string and a scalar and returns a list of values."

    ($order_time, $monk, $itemname, $quantity, $ignore) =
        unpack( "l i Z32 s2", $rec );

  Duncs

-----Original Message-----
From: Lars Noodén [mailto:lars.noo...@gmail.com] 
Sent: 31 March 2016 11:50
To: Perl Beginners <beginners@perl.org>
Subject: Processing binary data

What are the best practices for working with binary data?  If I read a
span of binary data, say, into $packet like with read() below

        my ( $packet_size, $packet );
        read( $client_socket, $packet_size, 4 );
        $packet_size = unpack( "N", $packet_size );

        read( $client_socket, $packet, $packet_size );

then how should I best access arbitrary ranges of bytes within that
block of binary data held by the $packet variable?

For example, put the first byte from $packet into one variable, the
second byte into another, the third through 18th into yet another, and
so on?

Regards,
Lars

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to