Portal | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
avaneendra_linga

New User

Joined: 13 Dec 2006
Posts: 73

 Posted: Thu Mar 08, 2007 3:53 pm    Post subject: Function for converting DECIMAL to BINARY in Cobol HI, IS THERE ANY FUNCTION TO CONVERT DECIMAL VALUE INTO BINARY IN COBOL?

William Thompson

Global Moderator

Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

 Posted: Thu Mar 08, 2007 4:36 pm    Post subject: In cobol, it is called a MOVE. 77 zoned-decimal pic s9(4)v9. 77 packed-decimal pic s9(4)v9 comp-3. 77 binary-decimal pic s9(4)v9 comp. MOVE 2.5 to zoned-decimal. MOVE zoned-decimal to packed-decimal. MOVE zoned-decimal to binary-decimal. MOVE packed-decimal to binary-decimal. Need more?
avaneendra_linga

New User

Joined: 13 Dec 2006
Posts: 73

 Posted: Thu Mar 08, 2007 4:44 pm    Post subject: this i understood William ........I NEED TO DISPLAY THE DECIMAL VALUE IN BINARY FORMAT.......
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19254
Location: Inside the Matrix

Posted: Thu Mar 08, 2007 9:32 pm    Post subject:

Hello,

What reason is there to create the binary number?

I am unclear on what this means:
 Quote: I NEED TO DISPLAY THE DECIMAL VALUE IN BINARY FORMAT

When you "display" a number, it needs to be visable to the eye. Most binary numbers internal representation is not visable. If you DISPLAY a binary number it will be converted from the internal representation so that it is visable.

If you'll clarify, we will be able to better help.
agkshirsagar

Active Member

Joined: 27 Feb 2007
Posts: 691
Location: Earth

Posted: Thu Mar 08, 2007 9:51 pm    Post subject:

Avaneendra,
See if this works-

 Code: WORKING-STORAGE SECTION.                                    77 BIN-VAR PIC X(16).                                      77 NUMER1 PIC 9(5).                                        77 I      PIC 99.                                          77 J      PIC 9.                                         PROCEDURE DIVISION.                                       MAIN-PARA.                                                     ACCEPT NUMER1.                                             PERFORM VARYING I FROM 16 BY -1 UNTIL I < 1                 DIVIDE NUMER1 BY 2 GIVING NUMER1 REMAINDER J               MOVE J TO BIN-VAR(I:1)                                   END-PERFORM.                                               DISPLAY BIN-VAR.

I hope this is what you needed.
I am curious where you will use this
Is this an assignment? Just curious
agkshirsagar

Active Member

Joined: 27 Feb 2007
Posts: 691
Location: Earth

 Posted: Thu Mar 08, 2007 9:53 pm    Post subject: NOTE: Above program will work only in range 0 to 65535 of variable NUMER1.
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19254
Location: Inside the Matrix

 Posted: Thu Mar 08, 2007 9:58 pm    Post subject: Hello, Where is the binary number (i.e. PIC 9(n) COMP)? The example will show 16 ones and zeros. If displaying the ones and zeros was the original request - very good catch
sarathmuruga
Currently Banned

New User

Joined: 04 Jan 2007
Posts: 1
Location: Kerala

 Posted: Fri Mar 09, 2007 11:07 am    Post subject: Re: Function for converting DECIMAL to BINARY in Cobol [code] IDENTIFICATION DIVISION. PROGRAM-ID. XYZ. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM1 PIC 9(18). 01 NUM2 PIC Z(18). 01 EXP PIC 9(5) VALUE IS 0. 01 R1 PIC 9(18). 01 R2 PIC 9(18) VALUE IS 0. PROCEDURE DIVISION. P1. DISPLAY " ENTER THE BINARY NUMBER ". ACCEPT NUM1. P2. DIVIDE NUM1 BY 10 GIVING NUM1 REMAINDER R1. COMPUTE R2 = R2 + R1 * 2 ** EXP ADD 1 TO EXP. IF NUM1 NOT = 0 GO TO P2 ELSE GO TO P3. P3. MOVE R2 TO NUM2. DISPLAY " THE DECIMAL NUMBER IS " NUM2. STOP RUN.
agkshirsagar

Active Member

Joined: 27 Feb 2007
Posts: 691
Location: Earth

 Posted: Fri Mar 09, 2007 11:27 am    Post subject: Will this program work if I enter 99999999999999999 for NUM1? Requirement was to convert 'Decimal to Binary' One more thing, while posting code post it with code tags.
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19254
Location: Inside the Matrix

 Posted: Sat Mar 10, 2007 12:11 am    Post subject: Hello, And there is still no binary number defined in the code? It may be that i just not understand the original request I wonder just what is the definition of "binary" in this topic. . . .
William Thompson

Global Moderator

Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

 Posted: Sat Mar 10, 2007 2:57 am    Post subject: It's been less than 12 hours, maybe we will hear from him again.... FWIW, re-reading his posts, I think Abhijit (I hate that avatar.. ) may have hit it, input a base 10 string of digits and output a display of base 2 ones and zeros. Hey, maybe cobolunni will come and play....
dick scherrer

Moderator Emeritus

Joined: 23 Nov 2006
Posts: 19254
Location: Inside the Matrix

 Posted: Sat Mar 10, 2007 4:25 am    Post subject: Well, it is binary. . . . Like i posted earlier, if displaying the ones and zeros was the goal - very good catch
William Thompson

Global Moderator

Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

 Posted: Sat Mar 10, 2007 4:30 am    Post subject: I do agree, he caught it well before I did.... Pending feedback from avaneendra_linga, good job Abhijit!
mmwife

Super Moderator

Joined: 30 May 2003
Posts: 1592

Posted: Sun Mar 11, 2007 2:21 am    Post subject:

I'm wondering if he wants to display the hex representation of a "decimal" number. Decimal is a bit ambiguous, but some examples:
 Code: External decimal 15 - hex rep - F1F5 Internal decimal 15 - hex rep - 015C or 015F Binary           15 - hex rep - 000F
avaneendra_linga

New User

Joined: 13 Dec 2006
Posts: 73