novair Back to Mainframe File Viewer
Mainframe File ViewerCOMP-3 packed decimal

COMP-3 packed decimal, explained byte by byte

COMP-3 stores a number as two decimal digits per byte, with the sign in the last half-byte. Once you can read the hex, you can read the number. Every value below was decoded by the extension from its bytes.

Have the file at hand? Open it in the free EBCDIC viewer online: hex and decoded text side by side, nothing uploaded

Two digits per byte, sign last

Each byte holds two half-bytes. Every half-byte is a digit from 0 to 9, except the very last one, which is the sign. Write the hex and read it left to right:

PIC S9(7)V99 COMP-3   00 01 23 45 6D   →  -1234.56
PIC S9(7)V99 COMP-3   00 01 23 45 6C   →   1234.56
PIC S9(7)V99 COMP-3   00 00 00 00 1C   →      0.01
PIC S9(5) COMP-3      12 34 5D         →    -12345
PIC S9(3)V99 COMP-3   12 34 5D         →   -123.45
PIC 9(5) COMP-3       12 34 5F         →     12345

The decimal point is not stored. V in the PIC says where it is: 12345D is -12345 for PIC S9(5) and -123.45 for PIC S9(3)V99. Without the copybook, nobody can tell which.

The sign half-byte

  • C: positive (signed field). D: negative.
  • F: unsigned, written by PIC 9 without S; read as positive.
  • A and E are alternative positive signs, B an alternative negative sign; the extension accepts them.
  • A digit 0 to 9 in the last half-byte is not a sign: the value is invalid.

How many bytes

Length in bytes = (number of digits + 1) / 2, rounded up. The sign takes the last half-byte, so an odd number of digits fills the field exactly:

PIC S9(5)     COMP-3   5 digits → 3 bytes
PIC S9(4)     COMP-3   4 digits → 3 bytes   (01 23 4C = 1234, a leading 0 pads)
PIC S9(7)V99  COMP-3   9 digits → 5 bytes
PIC S9(9)V99  COMP-3  11 digits → 6 bytes

Next to zoned and binary

The same copybook often mixes three ways of storing numbers. From the test file of the extension:

PIC S9(5)        zoned    F0 F0 F1 F0 C0   →       100   (sign in the first half of the last byte)
PIC S9(5)        zoned    F0 F0 F0 F0 D5   →        -5
PIC S9(4) COMP   binary   FF FE            →        -2   (two's complement, big-endian)
PIC S9(4) COMP   binary   04 D2            →      1234
PIC S9(7)V99     COMP-3   00 01 23 45 6D   →  -1234.56

Zoned digits are readable as EBCDIC text (F0 to F9 are the characters 0 to 9), except the last byte. Packed and binary bytes are not text at all: this is why a text conversion destroys them.

EBCDIC to ASCII: what converts, and what breaks

When COMP-3 goes wrong

Mainframe File Viewer marks an invalid packed value in the grid, counts it in the status bar, and F8 goes to the next one; the hex stays on screen. What the common cases look like:

00 01 2A 45 6C   invalid packed digit   A is not a digit: bad data in the file
00 01 23 45 65   invalid sign           the last half-byte is a digit, not C, D or F
0D 0A 23 45 65   invalid packed digit   0D 0A is a line end: a text-mode transfer
  • A few invalid values: the data itself is bad, often a field that a program never initialised. Walk through them with F8.
  • Every packed field of every record invalid: the offsets are wrong. The copybook does not match the file, the record length is wrong, or a VB file is read as FB (the 4-byte RDW shifts every field).
  • Packed values garbled and line ends in the file: the file was transferred in text mode. It must be sent again in binary mode; the extension warns about it when the file opens.

Frequently asked questions

What is the difference between COMP and COMP-3?

COMP (also COMP-4, BINARY) is a binary integer of 2, 4 or 8 bytes. COMP-3 (PACKED-DECIMAL) stores decimal digits, two per byte, with a sign half-byte. COMP-3 keeps decimal amounts exact, which is why it holds most money fields.

Why does my COMP-3 value look like garbage in a text editor?

Because its bytes are not characters. 00 01 23 45 6D shows as control characters and stray symbols. It must be decoded with the PIC from the copybook: Mainframe File Viewer shows -1234.56 next to 000123456D.

Does a code page change COMP-3 values?

No. Packed and binary fields are numbers, the same in every code page. Only text fields depend on the code page.

Read your own files the same way

Mainframe File Viewer for VS Code reads your EBCDIC files with their copybook, on your machine, with the hex next to every value. Free, no account; Pro adds several layouts per file, VB and OCCURS DEPENDING ON cut into fields, comparison details and CSV and JSON export.

A format or an in-house rule of your own? We build it in five working days, at a fixed price: EUR 490 + VAT where applicable, per work package. See the work packages