Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read binary file with known structure

Posted on 2009-04-03
2
Medium Priority
?
1,640 Views
Last Modified: 2012-08-14
Hello,
I have to get a string from binary file. But to get it have to parse the file. Fortunately it's structure is known, but some parts size defined by integer numbers in the file. So it goes like this:
integer (4 bytes, little-endian)
integer
unsigned short (2 bytes)
unsigned short
integer
char [length defined by the integer above]
integer
char [length defined by the integer above]
...
...
I need to get the last string of chars above (it's not the last in the file).
My problem is I cannot get integers out of binary stream to determine the size of string after it.
Would you help please? Please use only standard Perl, no libraries.
0
Comment
Question by:yuk99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24064514
You can use the pack and unpack function to interact with the data.  With these functions, you give them a format code, and they convert from/to data data.

The exact format codes will depend on your data.  For details, see here:
    http://perldoc.perl.org/functions/pack.html
A summary:
  v    An unsigned short (16-bit) in "VAX" (little-endian) order.
  V    An unsigned long (32-bit) in "VAX" (little-endian) order.

##### To get just the first integer
read($FH, $string, 4);   #Read 4 bytes into $string (assumes $FH is your file handle)
my $number = unpack("V", $string);
 
#To get:
#  integer (4 bytes, little-endian)
#  integer  [I'm assuming same type: 4 bytes little-endian]
#  unsigned short (2 bytes)
#  unsigned short
#  integer  [I'm assuming same type: 4 bytes little-endian]
read($FH, $string, 16);  #Read 16 bytes
my ($int1, $int2, $short1, $short2, $int3) = unpack("VVvvV", $string);

Open in new window

0
 
LVL 15

Author Closing Comment

by:yuk99
ID: 31566431
Adam, thank you so much! Works great!
I heard I should use unpack, but could not understand how to use it. You made my day. Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question