# Convert Packet decimals comp-3 and comp-6

Peter W A Wood peterwawood at gmail.com
Fri May 6 06:23:34 EDT 2011

```Hi Josep

On 6 May 2011, at 00:36, JosepM wrote:

> Hi,
>
> I need to read a file from a Cobol application and import the data files.
> I have the file description but I don't have idea how convert the decimals
> fields.
>
> 01 NAME PIC X(30) --> This I guess is 30 chars
> 02 CODE PIC 9(5) COMP-6 --> This is five digits with packet decimal format.

I haven't heard of COMP-6 though it is a very long time since I last wrote a COBOL programme, probably the early nineties. I managed to find this "The format of a COMP-6 item is identical to a COMP-3 item except that it is unsigned and no space is allocated for the sign. Thus there are two decimal digits per byte, and the actual size of the item is determined by dividing its PICTURE size by two and rounding up. " at http://ibmmainframes.com/about146.html

> Comp-3 stores data in a BCD -- binary coded decimal -- format with the sign
> after the least significant digit.

I would guess that the COMP-6 field would be

Bits 1-4     : First Digit
Bits 5-8     : Second Digit
Bits 9-12   : Third Digit
Bits 13-16 : Fourth Digit
Bits 17-20 : Fifth Digit
Bits 21-24 : to be ignored.

From the PIC 9(5) the field is an integer.

So the code to extract the value would need to be something along the following lines. It assumes that you treat the COBOL data as a string. I'm afraid that I haven't been able to test it.

set tCode to 0
# first byte
put char 1 of tCOBOLCODE into tChar
# first digit
put charToNum(tChar) bitAnd 240 into tInt
divide tInt by 8
multiply tCode by 10
# second digit
put charToNum(tChar) bitAnd 15 into tInt
multiply tCode by 10
# second byte
put char 2 of tCOBOLCODE into tChar
# third digit
put charToNum(tChar) bitAnd 240 into tInt
divide tInt by 8
multiply tCode by 10
# fourth digit
put charToNum(tChar) bitAnd 15 into tInt
multiply tCode by 10
# third byte
put char 3 of tCOBOLCODE into tChar
# fifth digit
put charToNum(tChar) bitAnd 240 into tInt