phpmainframebcdpacked-decimal

Unpack Mainframe packed Decimal (BCD) with PHP


I got an data file from a mainframe. I handled already the EBCDIC conversion to latin1 with PHP. But now are this packed decimal fields left.

For examle the number 12345 is packed into 3 Bytes and looks like: x'12345C'

Negative would be like: x'12345D'

So the right half Byte telling the sign. Is there a way to do this easily with PHP?

Now I do that like:

$bin = "\x12\x34\x5C";
var_dump(
    unpack("H*", $bin)
);

It results in:

array(1) {
  [1]=>
  string(4) "123c"
}

Now I could check if last sign is C or D and do all by hand. But maybe there is a nicer solution?


Solution

  • As Bill said, get the mainframe people to convert the file to Text on the Mainframe and send the Text file, utilities like sort etc can do this on the mainframe. Also is it just packed decimal in the file or do you have either binary or Zoned Decimal as well ???

    If you insist on doing it in PHP, you need to do the Packed Decimal conversion before you do the EBCDIC conversion because for a Packed-decimal like x'400c' the EBCDIC converter will look at the x'40' and say that is a space and convert it to x'20', so your x'400c' becomes x'200c'.

    Also the final nyble in a Packed-decimal can be f - unsigned as well as c and d.

    Finally if you have the Cobol Copybook, my project JRecord has Cobol to Csv && Cobol to Xml conversion programs (Written in java). See