Solved

Read binary file with known structure

Posted on 2009-04-03
2
1,637 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

738 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