perlquestion
dbarkho14
<p>I am converting IBM mainframe files and I have had a lot of luck
converting binary fixed length files that have COMP-3 fields using the unpack ("H*",$_) setup, however
I am having an issues with a file that has binary fields.
Here is the input record layout:</p>
<p>01 EXTRACT-REC-IN.<br>
03 KEY-DATA.<br>
05 FIELD1 PIC 9(5) VALUE 0 BINARY.<br>
05 FIELD2 PIC 9(4) VALUE 0 BINARY.<br>
05 FIELD3 PIC 9(4) VALUE 0 BINARY.<br>
05 FIELD4 PIC 9(8) VALUE 0 BINARY.<br>
05 FIELD5 PIC 9(4) VALUE 0 BINARY.<br>
03 DATA.<br>
05 FIELD6 PIC S9(04) VALUE 0 COMP.<br>
05 FILED7 PIC S9(15)V99 VALUE 0 COMP-3.<br>
05 FIELD8 PIC S9(15)V99 VALUE 0 COMP-3.</p>
<p>How do I unpack these BINARY fields in the KEY-DATA Section?
See my code below as I have tried to use I1 in unpack section but it gives
me 2936078336 when I am expecting to see 425 for FIELD1
Any help is appreciated!</p>
<code>
#! /usr/bin/perl -w
@ARGV == 1 or die "usage: $0 in_filename out_filename\n";
my $in_filename = shift;
#set infile to binary mode
open INFILE, '<:raw', $in_filename or die "can't open $in_filename: $!";
binmode(INFILE);
#open OUTFILE, '>', $out_filename or die "can't open $out_filename: $!";
# record length is 34
$/ = \34;
#map input file to process integers
while ( <INFILE> ) {
my($f1)
= unpack ("I1",$_);
#format variables
my $f1p = sprintf("%d", $f1);
#write record to file
print "$f1p\n";
}
close INFILE;</code>