## convert 1 BYTE binary to decimal in cobol

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

### convert 1 BYTE binary to decimal in cobol

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.
bitbybit

Posts: 29
Joined: Fri Jul 13, 2012 10:05 pm
Has thanked: 6 times
Been thanked: 0 time

### Re: convert 1 BYTE binary to decimal in cobol

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.

These users thanked the author Robert Sample for the post:
bitbybit (Thu Nov 08, 2012 8:35 am)
Robert Sample
Global moderator

Posts: 3686
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 274 times

### Re: convert 1 BYTE binary to decimal in cobol

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
bitbybit

Posts: 29
Joined: Fri Jul 13, 2012 10:05 pm
Has thanked: 6 times
Been thanked: 0 time

### Re: convert 1 BYTE binary to decimal in cobol

Hi Robert Sample...That wrks for 3 digit numbers too...Thnx a bunch...
bitbybit

Posts: 29
Joined: Fri Jul 13, 2012 10:05 pm
Has thanked: 6 times
Been thanked: 0 time

### Re: convert 1 BYTE binary to decimal in cobol

the post seems a duplicate of

dfsort-icetool-icegener/topic8430.html
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2978
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 160 times

### Re: convert 1 BYTE binary to decimal in cobol

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.
bitbybit

Posts: 29
Joined: Fri Jul 13, 2012 10:05 pm
Has thanked: 6 times
Been thanked: 0 time