Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have a binary file (think ELF or Windows PE) that includes fields that may be long (32 bit) or quad (64) bit and little or big endien depending on the specific file. The script may be running on a big or little endian machine with a Perl built for 32 or 64 bits. I want to unpack the fields for later use that will include display using printf and file operations using read and seek so I need to convert from file representation to the running Perl's native representation for those fields. The following sample code does that, but the pack 'L2' ...; unpack 'Q' feels a bit clunky. Can it be tidied up?

use warnings; use strict; use Config; use Fcntl; printf "Perl $^V %s ivsize %d byteorder %d\n", $Config{archname}, $Config{ivsize}, $Config{byteorder}; # Generate a "binary file" with a string of bytes likely to show up is +sues in # decoding my $binary = "\x91\x34\x33\x90\x81\x32\x31\x80"; for my $fileLE (0, 1) { my $fromFile = $fileLE ? 'V4' : 'N4'; open my $inFH, '<:raw', \$binary; read $inFH, my $raw1, 4; read $inFH, my $raw2, 4; my $long1 = unpack $fromFile, $raw1; my $long2 = unpack $fromFile, $raw2; my $packed = pack "L2", $fileLE ? ($long1, $long2) : ($long2, $lon +g1); my $longlong = unpack 'Q', $packed; printf "%s: %016x\n", ($fileLE ? "LE" : "BE"), $longlong; }

A 32 bit Windows build prints:

Perl v5.32.0 MSWin32-x86-multi-thread-64int ivsize 8 byteorder 1234567 +8 BE: 9134339081323180 LE: 8031328190333491

A nice sanity check would be to run the code on a big endian system and see that the numbers generated are the same. Of course the long long processing will come unstuck where ivsize < 8.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Unpacking byte stream long/quad little/big endian fields by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found