Reading COMP-3 packed decimal fields from mainframe files
COMP-3, also called packed decimal, is a COBOL storage format that holds two decimal digits in each byte. The last half-byte holds the sign instead of a digit. A five-digit amount takes three bytes, which saves space on a mainframe but makes the field unreadable in a text editor or a database import tool. The decimal point is not stored at all. It is implied by the PIC clause in the copybook, so a field declared as PIC S9(5)V99 COMP-3 has to be read as seven digits with two of them after the decimal point.
TextPipe decodes these fields for you. You point it at the data file and the copybook, or enter the field offsets and lengths by hand, and it writes each packed field as an ordinary number with the correct sign and decimal point. The surrounding text fields are converted from EBCDIC to ASCII in the same pass, so the output is a complete, readable record.
Sample input and output
A 4-byte field declared as PIC S9(6)V9 COMP-3 holds the bytes below, shown in hex:
12 34 56 7C (positive)
12 34 56 7D (negative)
The digits are 1234567 and the final half-byte is the sign: C means positive, D means negative and F means unsigned. With one implied decimal place, TextPipe writes:
123456.7
-123456.7
A full record with a customer name, an account number and a packed balance would come out as a single CSV line such as SMITH J,00481516,123456.7, ready for SQL Server, Oracle or Excel.
Typical settings
- Field position and length: the byte offset and the packed length of each COMP-3 field, taken from the copybook.
- Decimal places: the number of digits after the implied decimal point, from the V in the PIC clause.
- Record length: fixed record size for files without line endings, which is typical of mainframe extracts.
- Output format: CSV, tab delimited or fixed width text, with or without a header line.
Practical applications
Banks and insurers use the conversion to load account balances and premium tables from legacy systems into reporting databases. Auditors use it to check mainframe totals against a PC copy of the data. Migration teams use it to retire an old application while keeping its history searchable.
Common pitfalls
- Do not run an EBCDIC to ASCII conversion over the whole record before decoding the packed fields. The conversion changes the bytes inside the packed fields and corrupts the numbers. Decode packed fields first, or convert only the character fields.
- A wrong decimal place count shifts every value by a power of ten, and the output still looks valid. Compare one record with a printout from the source system.
- FTP transfers in text mode alter binary bytes. Transfer the file in binary mode.
Related pages
The COMP-3 packed decimal guide covers the field format in more depth. For files with full copybooks, see the COBOL copybook import page. Character fields need the EBCDIC to ASCII converter, and the results can be ordered with large file sorting. The data conversion solutions page and the banking and finance case study show these steps in larger projects.