Page 1 of 1

How to Pack Char data in cobol

PostPosted: Thu Sep 13, 2012 5:12 pm
by Mann_B
Hi All

Can we pack character data ..
If in input we have X(04) fld with data ABCD , i need to pack it to below whose def is X(02).
AC
BD
without changing the value.
Can you please help me out.

Re: How to Pack Char data in cobol

PostPosted: Thu Sep 13, 2012 5:44 pm
by Robert Sample
Your "requirement" is not clear -- what do you want to do if the PIC X(4) data is 'WXYZ'? -- which cannot be stored, at all, in a 2-byte variable? For the general case, a 4-byte character field cannot be stored in a 2-byte character field without using some encoding method (Google Huffman coding for one example of how it can be done).

If you are talking about numeric data only (0 to 9 and possibly the hexadecimal values A,B,C,D,E,F), what have you tried and where are you having problems? Have you read the COBOL Language Reference manual (link at the top of this page) on internal representations of data and used that to get an idea how to proceed?

Re: How to Pack Char data in cobol

PostPosted: Thu Sep 13, 2012 11:35 pm
by dick scherrer
Hello,

This appears to be just a continuation of your other topic about the same situation.

Now, as then, you have not clearly posted what you are tryinig to do.

Show several "input" values and how you want these values stored by your peocess. Explain what should happen if the input data is MNTR.

Re: How to Pack Char data in cobol

PostPosted: Tue Sep 18, 2012 1:25 pm
by Mann_B
HI

My input have characters only from A-F and numebers.
Sample is 7AFB016G.I need to pack this as..
7F06
AB1G

Re: How to Pack Char data in cobol

PostPosted: Tue Sep 18, 2012 1:46 pm
by BillyBoyo
Well, you are stuck then. G is not valid as a representation of a value in hexadecimal notation.

Re: How to Pack Char data in cobol

PostPosted: Tue Sep 18, 2012 1:53 pm
by NicC
And G is not in the range A-F (unless someone changed the English alphabet overnight)

Re: How to Pack Char data in cobol

PostPosted: Tue Sep 18, 2012 2:11 pm
by Mann_B
oops ...I am sorry,Sample is 7AFB016C.

Re: How to Pack Char data in cobol

PostPosted: Tue Sep 18, 2012 3:31 pm
by BillyBoyo
There is more than one way to do this.

Note that you have 16 values (0-9, A-F). These have a hex representation as characters F0-F9, C1-C6, and also a hex representation when "packed" in a byte - or two, one when on the "left" and one when on the right - ?0-?9, ?A-?F and 0?-9?, A?-F?.

So, at the heart, there can all come down to a simple 1-to-1 relationship.

Then you have some technical stuff, like you can't access a single byte as a binary value in Cobol.

01  a-nice-name COMP-5 PIC 9(4).
01  FILLER REDEFINES a-nice-name,
    05  FILLER PIC X.
    05  another-nice-name-for-this-byte PIC X.