Search code examples
asciimainframeebcdicpacked-decimal

How to unpack COMP digits using Java?


I had found this useful link for unpacked COMP-3 digit, but i need to unpack COMP digit this time, is anyone know how to unpack it? Thanks a lot!


Solution

  • In most Cobol compilers Comp is a big endian binary integer. For the mainframe only 2/4/8 bytes are supported. So For signed values

      03  Signed-Num     pic s9(4) comp.
    

    if you have the value in an array of bytes you can do

    BigInteger value = new BigInteger(byteArray);
    

    Alternatively you could use the readShort(), readInt() and readLong() methods of DataInputStream

    Finally JRecord will let you read Cobol files with a Cobol copybook