Solved

Read binary file with known structure

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now