http://qs321.pair.com?node_id=11116442

dbarkho14 has asked for the wisdom of the Perl Monks concerning the following question:

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:

01 EXTRACT-REC-IN.
03 KEY-DATA.
05 FIELD1 PIC 9(5) VALUE 0 BINARY.
05 FIELD2 PIC 9(4) VALUE 0 BINARY.
05 FIELD3 PIC 9(4) VALUE 0 BINARY.
05 FIELD4 PIC 9(8) VALUE 0 BINARY.
05 FIELD5 PIC 9(4) VALUE 0 BINARY.
03 DATA.
05 FIELD6 PIC S9(04) VALUE 0 COMP.
05 FILED7 PIC S9(15)V99 VALUE 0 COMP-3.
05 FIELD8 PIC S9(15)V99 VALUE 0 COMP-3.

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!

#! /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;