Hi..
This is my program but I have seen that the substr is not subtring the data correctly. Please help me
#!/usr/bin/perl -w
use Cwd;
use warnings;
use strict;
use Getopt::Long;
use constant FILEHDR => 4;
use constant CDRLEN => 286;
my ($trace, $help, $infile);
my $swap = '';
my $indir = getcwd;
my $outdir = getcwd;
GetOptions (
"h|help" => \$help,
"filename|f=s" => \$infile,
"swap|s" => \$swap,
"input|i=s" => \$indir,
"output|o=s" => \$outdir,
"trace|t" => \$trace
) or usage();
sub usage {
exit;
}
my $outfile = $infile;
my $data;
if ($infile) {
#open (OUTPUT, ">$outdir/$outfile");
open (DATA, "$indir/$infile");
binmode DATA;
while ($data = <DATA>) {
my $tag = unpack "H2", substr $data,0,1,'';
my $length = unpack "C", substr $data,0,1,'';
if ($length == 0x81) {
$length = unpack "C", substr $data,0,1,'';
}
# Problem is here.. data not substr correctly
my $template = 'H' . $length*2;
my $rec = unpack $template, substr $data,0,$length,'';
printf ("RECORD TAG : %s\n", $tag);
printf ("RECORD LENGTH : %s\n", $length);
printf ("RECORD : %s\n", $rec);
}
close(DATA);
#close(OUTPUT);
}
This is sample file
84 47 00 0c 00 00 11 0a 03 50 35 04 00 64 0a 04
16 12 00 1e 00 1d 00 00 65 09 08 54 52 03 2f 82
05 10 00 02 6e 06 0b a8 53 11 67 00 00 00 7a 7f
00 69 4a 42 47 48 4a 41 0c 00 6a 53 54 4d 44 54
4f 1c 00 66 04 00 00
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.