jayw:

If your file is typical of the ones I work with, you're not going to do it with a simple one-liner. (Unless, of course, you don't mind long lines.)

The COMP-3 format is basically a packed BCD scheme with a trailing sign indicator. So each data byte will contain two digits (or a digit and the sign for the last byte) of your number. So if you take a number, say 54106 and you want to put it into a PIC 9(7) COMP-3 field, you:

  1. Convert the number into a string, and pad it with leading zeroes to 7 (the number of digits in your field) characters: "0054106".
  2. Add the sign character to the end (I typically use A for unsigned, B or D for negative, and C or E or F for positive).
  3. Now you have a string representation of a hexadecimal number, four bytes long, that contains your value: "0054106C".
  4. Then convert the string to binary for the field, and you've finished that field.

(Converting that to code, adding error checking, etc., is left as an exercise for the reader.)

The main problem is that you need to pack in multiple COMP-3 fields, perhaps some floating point values (trickier to convert!) and perhaps some EBCDIC conversion as well. What fun!

I decode these sorts of files all the time. Tell me "I want your crappy C program to decode a mainframe file", and give me your EMail address, and I'll ship it to you. (I'm flying out of town and will be back Tuesday evening, so don't expect a reply before Wednesday morning.)

...roboticus

In reply to Re: ASCII text on UNIX (Sun/Solaris 10) to MVS packed decimal (comp-3) using perl pack inline command by roboticus
in thread ASCII text on UNIX (Sun/Solaris 10) to MVS packed decimal (comp-3) using perl pack inline command by jayw

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.