Page 1 of 1

convert 1 BYTE binary to decimal in cobol

PostPosted: Thu Nov 08, 2012 7:00 am
by bitbybit
I have a 1 byte field PIC x(1) in my input file, but the value is binary format (8 bits), how do I convert the binary value to decimal (display on a report) in a COBOL pgm?
For example, if that one byte has 1010011, I should display on report a value 83.

Re: convert 1 BYTE binary to decimal in cobol

PostPosted: Thu Nov 08, 2012 7:19 am
by Robert Sample
One method would be to:
Define a 2-byte alphanumeric variable (group or elementary).
Redefine the 2-byte alphanumeric variable as PIC S9(04) COMP.
Move LOW-VALUES to the first byte of the 2-bytes variable.
Move the 1-byte to the second byte of the 2-byte variable (directly if the 2-byte variable is a group of 2 1-byte variables, or via reference modification otherwise).
The PIC S9(04) COMP variable contains your value.

Re: convert 1 BYTE binary to decimal in cobol

PostPosted: Thu Nov 08, 2012 8:39 am
by bitbybit
Hi Robert Sample...that worked...thank you...But, if the decimal value is more than 2 digits, is it still going to work?
For ex: if binary value is : 10010100 , teh decimal value shld be 148 ?
The sample file I have only has records that have 2 digit values and I do not know how to manipulate input data at the bit level...but, input can have values upto 255

Re: convert 1 BYTE binary to decimal in cobol

PostPosted: Thu Nov 08, 2012 8:50 am
by bitbybit
Hi Robert Sample...That wrks for 3 digit numbers too...Thnx a bunch...

Re: convert 1 BYTE binary to decimal in cobol

PostPosted: Thu Nov 08, 2012 2:39 pm
by enrico-sorichetti
the post seems a duplicate of

dfsort-icetool-icegener/topic8430.html

Re: convert 1 BYTE binary to decimal in cobol

PostPosted: Tue Nov 13, 2012 7:49 pm
by bitbybit
Hi enrico-sorichetti, I posted both questions, But, they r not same. This one, I am asking how to convert binary to decimal in a Cobol program for display purposes. The other one is a DFsort/ICEtool question about how I can sort by binary flds and mainly to count #of records for each fld1/fld2 combinations. Totally different questions. Solution by Robert sample wrked here and I havent gotten any solution for the other one yet. Meanwhile I had to write a quick cobol pgm that counts records for each fld1/fld2 combination and spits them to a report.