Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Perl Unpack Cobol Binary File and Fields

by sundialsvc4 (Abbot)
on May 06, 2020 at 23:19 UTC ( #11116524=note: print w/replies, xml ) Need Help??


in reply to Perl Unpack Cobol Binary File and Fields

"It's all coming back to me now." Here goes:
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.
Your data should consist of big-endian data as follows:
  1. FIELD1, FIELD4 four unsigned bytes, binary.
  2. FIELD2, FIELD3, FIELD5 two unsigned bytes each, binary.
  3. DATA.FIELD6 two bytes, binary, but two's complement signed.
  4. DATA.FIELD7-FIELD8 nine-byte fields consisting of seventeen binary-coded-decimal (BCD) digits (four bits = "one nybble" each) followed by a sign-indicator in the rightmost "nybble" as described above. The rightmost two digits are implicitly understood to be right of the decimal point.

I am not immediately aware of any Perl package which will directly handle "BCD representation." Although I do, of course, expect that Many Monks will promptly correct me on this point! Because, "this being Perl, surely it must be there."

Edited:   Thanks for pointing out what I originally had missed – that FIELD4 requires to be a four-byte field.

Replies are listed 'Best First'.
Re^2: Perl Unpack Cobol Binary File and Fields
by soonix (Canon) on May 07, 2020 at 12:07 UTC
    You missed that FIELD4 has PICTURE 9(8) which doesn't fit in two bytes.

    But, you are right, the modules for BCD do exist, as you expected, however, they are not the easiest to find.

    Meanwhile, dbarkho14 probably has his problem solved, but here's my take on it, anyway:
    (test data from Re^2: Perl Unpack Cobol Binary File and Fields)

    use 5.011; use warnings; use Data::Dumper; my $rec = "\0\0\1\257\a\344\0\3\0\0\0s\0\6\2\267\0\0\0\0\0\0\5E\35\0\0 +\0\0\0\0\0\0\f"; say "Length is ", length $rec; my @fields = unpack "l>s>s>l>s>s>H18H18", $rec; say Dumper @fields; say Dumper map { my ($value, $sign) = unpack "A17A"; $value = ( $sign =~ /[BD]/i ? -1 : +1 ) * $value; } @fields[-2 .. -1];
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11116524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2020-09-29 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I don’t succeed, I …










    Results (146 votes). Check out past polls.

    Notices?