Hi Monks, I have a binary file with the headder content (in HEX) as below (one single line).
0001000000004f914f1c0b010042534341303000000005000043413030303030000000 +014341303030313400000002434130303032320000000343413030303234000000044 +34130303032390000000100224e43656c6c3000000002004253434130310000000500 +054341303130303100000006434130313030340000000743413031303130000000084 +341303130313500000009434130313031360000000100224e43656c6c300000000300 +42534341303200000006000a
This data should be unpacked to populate the following structure.
Header Record Id : 1 Byte File Format Version : 1 Byte Timestamp : 8 Bytes No. of BSCs : 1 Byte For each BSC ... BSC Id : 1 Byte Application Version : 1 Byte BSC Name : 2 Bytes Number of Cells : 2 Bytes For each Cell ... Cell Pointer : 2 Bytes Cell Name : 9 Bytes Number of Neighbour Cells : 2 Bytes For each Neighbour Cell to this BSC ... Cell Pointer : 2 Bytes Cell Name : 9 Bytes

Here is what I have been trying...

#! /usr/bin/perl -w open(FILE, "<A20120420.1257+0100-1302+0100_group0.bin"); binmode(FILE); my $headder = <FILE>; ($headderRecordId, $fileFormatId, $timeStamp, $noOfBSCs, $specOfBSCs) += unpack ("H2 H2 H16 H2 H*", $headder);

No doubt, I can decode the binary file this way. But here is what I prefer:

1. I need to convert the $noOfBSCs to Decimal, and then use it in a loop and then decode the data for the rest of the BSCs. Same would be followed for the Cells also.

> Is there a way that I can work without converting the read hex value in loops?

2. I do see that there is dynamic unpack possible also. But then I could not understand.

Since performance is a criteria, I dont want to code with C kind of logic. Please let me know as to how it can be done better in perl. Thanks in advance, PerlJedi


In reply to Handling Hex data with Dynamic unpack by PerlJedi

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.