COBOL Data Type File Formatting

Since a lot of existing COBOL jobs take flat files as input, and produce flat files, I've been doing some experimenting with data manipulation in COBOL friendly formats. Much of the formatting is pretty straight forward, if you have a variable defined as PIC X(10), it's going to take up 10 bytes in the flat file, and likely be right padded with spaces. And, being that my company runs on a z/OS platform, it's going to use the EBCDIC character encoding.

However, where it gets interesting is the formatting of numbers using signs, decimal points, and binary data. I wrote a little script that uses JZOS to format data into a copybook equivalent byte stream and tried a couple simple test cases to see what happened.

['99V99', 'S99V99', 'S99V99 COMP-3'].each { format ->
 def slurper = new CopybookSlurper("""
 01 TESTING.
    03 BEGIN   PIC X(6).
    03 MIDDLE  PIC $format.
    03 END     PIC X(4). """)

 def writer = slurper.getWriter(new byte[slurper.length])
 writer.BEGIN = 'BEGIN|'
 writer.MIDDLE = 43.86
 writer.END = '|END'
 println new String(writer.getBytes(), 'IBM-37')
}

After the script ran, I had the following outputs.

BEGIN|4386|END
BEGIN|438F|END
BEGIN|??%|END

So, the decimal point is assumed, it doesn't show up in the output at all. When I add the 'S' to have a sign, it changes the last digit in the number. Half of the byte is used to determine the sign, so the '6' gets converted in a binary value, which in this case happens to print as a 'F'. And of course binary data doesn't really look like anything if you try to print it as a String, it doesn't even reliably take up the same number of characters.

I probably knew this at one point, back when I was in COBOL training, but it was helpful to have a refresher course.

Post a Comment