Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read binary file with known structure

Posted on 2009-04-03
2
Medium Priority
?
1,642 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

963 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