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;

In reply to Perl Unpack Cobol Binary File and Fields by dbarkho14

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.