Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Handling Hex data with Dynamic unpack

by Marshall (Canon)
on Jul 05, 2012 at 11:16 UTC ( [id://980031]=note: print w/replies, xml ) Need Help??


in reply to Handling Hex data with Dynamic unpack

By printing this out in ASCII hex, you have actually changed the problem.

When you do a binmode read to a scalar, you get a $buffer where each "character" is a byte (0-255 unsigned). For actual bytes there is "nothing to be done" - it is already a byte value. For multi-byte fields some sort of unpack() is usually necessary. Use substr() to get a range of byte values. Use unpack() to convert sequences of bytes into some other representation (from little endian to big endian or whatever).

my $HeaderRecordId = substr($buf,0,1); my $FileFormatVersion = substr($buf,1,1); my $TimeStamp = substr($buf,2,8); #some kind of unpack needed here! my $NumBSC = substr($buf,10,1);
$HeaderRecordId $FileFormatVersion, $NumBSC are just bytes and nothing more is needed past substr().

Update: I looked back an some ancient code (I don't deal with binary very often), but this had to do with .WAV files. My point is that substr() will get you the sequence of bytes. Here, I look for "RIFF" and "data" with string compares. The V4 unpack is for little endian conversion.

code snippet... read(IN, my $buff, 1 * 2**10); (substr($buff,0,4) eq "RIFF") || die "not a valid RIFF file"; my $size = unpack ("V4",substr($buff,4,4)); myprint (" RIFF segment size = $size"); (substr($buff,50,4) eq "data")|| die "data segment not found"; my $dsize = unpack ("V4",substr($buff,54,4)); myprint (" DATA Segment size = $dsize");

Replies are listed 'Best First'.
Re^2: Handling Hex data with Dynamic unpack
by Anonymous Monk on Jul 05, 2012 at 11:20 UTC

    For actual bytes there is "nothing to be done" - it is already a byte value.

    Except unpacking -- like if you want get an actual number (big-endian 8-bit signed integer )

Re^2: Handling Hex data with Dynamic unpack
by patcat88 (Deacon) on Jul 05, 2012 at 18:36 UTC
    binmode does not work on a scalar, only a file handle. substr can be either in utf8 mode or byte mode, and substr doesn't guarantee the returning scalar is in one or the other mode.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found