Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found