title: Unpack Mainframe packed Decimal (BCD) with PHP
body: <p>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.</p>
  
  <p>For examle the number 12345 is packed into 3 Bytes and looks like: x'12345C'</p>
  
  <p>Negative would be like: x'12345D'</p>
  
  <p>So the right half Byte telling the sign. Is there a way to do this easily with PHP?</p>
  
  <p>Now I do that like:</p>
  
  <pre><code>$bin = "\x12\x34\x5C";
  var_dump(
      unpack("H*", $bin)
  );
  </code></pre>
  
  <p>It results in:</p>
  
  <pre><code>array(1) {
    [1]=&gt;
    string(4) "123c"
  }
  </code></pre>
  
  <p>Now I could check if last sign is C or D and do all by hand. But maybe there is a nicer solution?</p>