Solved

Read binary file with known structure

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

10 Experts available now in Live!

Get 1:1 Help Now