Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Function for converting DECIMAL to BINARY in Cobol

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
avaneendra_linga

New User


Joined: 13 Dec 2006
Posts: 73
Location: Hyderabad

PostPosted: Thu Mar 08, 2007 3:53 pm    Post subject: Function for converting DECIMAL to BINARY in Cobol
Reply with quote

HI,
IS THERE ANY FUNCTION TO CONVERT DECIMAL VALUE INTO BINARY IN
COBOL?
Back to top
View user's profile Send private message

William Thompson

Global Moderator


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

PostPosted: Thu Mar 08, 2007 4:36 pm    Post subject:
Reply with quote

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?
Back to top
View user's profile Send private message
avaneendra_linga

New User


Joined: 13 Dec 2006
Posts: 73
Location: Hyderabad

PostPosted: Thu Mar 08, 2007 4:44 pm    Post subject:
Reply with quote

this i understood William ........I NEED TO DISPLAY THE DECIMAL VALUE IN BINARY FORMAT.......
Back to top
View user's profile Send private message
dick scherrer

Site Director


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

PostPosted: Thu Mar 08, 2007 9:32 pm    Post subject:
Reply with quote

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.
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 686
Location: Earth

PostPosted: Thu Mar 08, 2007 9:51 pm    Post subject:
Reply with quote

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 icon_question.gif
Is this an assignment? Just curious icon_biggrin.gif
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 686
Location: Earth

PostPosted: Thu Mar 08, 2007 9:53 pm    Post subject:
Reply with quote

NOTE:
Above program will work only in range 0 to 65535 of variable NUMER1. icon_wink.gif
Back to top
View user's profile Send private message
dick scherrer

Site Director


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

PostPosted: Thu Mar 08, 2007 9:58 pm    Post subject:
Reply with quote

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 icon_biggrin.gif
Back to top
View user's profile Send private message
sarathmuruga
Currently Banned

New User


Joined: 04 Jan 2007
Posts: 1
Location: Kerala

PostPosted: Fri Mar 09, 2007 11:07 am    Post subject: Re: Function for converting DECIMAL to BINARY in Cobol
Reply with quote

[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.
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 686
Location: Earth

PostPosted: Fri Mar 09, 2007 11:27 am    Post subject:
Reply with quote

icon_confused.gif
icon_surprised.gif
icon_exclaim.gif
Will this program work if I enter 99999999999999999 for NUM1? icon_lol.gif

Requirement was to convert 'Decimal to Binary'
One more thing, while posting code post it with code tags. icon_neutral.gif
Back to top
View user's profile Send private message
dick scherrer

Site Director


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

PostPosted: Sat Mar 10, 2007 12:11 am    Post subject:
Reply with quote

Hello,

And there is still no binary number defined in the code?

It may be that i just not understand the original request icon_confused.gif

I wonder just what is the definition of "binary" in this topic. . . .
Back to top
View user's profile Send private message
William Thompson

Global Moderator


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

PostPosted: Sat Mar 10, 2007 2:57 am    Post subject:
Reply with quote

It's been less than 12 hours, maybe we will hear from him again.... icon_smile.gif

FWIW, re-reading his posts, I think Abhijit (I hate that avatar.. icon_rolleyes.gif ) 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.... icon_lol.gif
Back to top
View user's profile Send private message
dick scherrer

Site Director


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

PostPosted: Sat Mar 10, 2007 4:25 am    Post subject:
Reply with quote

Well, it is binary. . . . icon_smile.gif

Like i posted earlier, if displaying the ones and zeros was the goal - very good catch icon_smile.gif
Back to top
View user's profile Send private message
William Thompson

Global Moderator


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

PostPosted: Sat Mar 10, 2007 4:30 am    Post subject:
Reply with quote

I do agree, he caught it well before I did....
Pending feedback from avaneendra_linga, good job Abhijit!
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Mar 11, 2007 2:21 am    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
avaneendra_linga

New User


Joined: 13 Dec 2006
Posts: 73
Location: Hyderabad

PostPosted: Sun Mar 11, 2007 10:48 am    Post subject: hi
Reply with quote

hi folks.....
thx a lot........the logic that was provided by agkshirsagar will do my requirement....
requirement: i need to display the decimal value in binary format ..(ex:1010101).......
anyway thx a lot folks for your immediate responces......
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts XML Parse for COBOL 5.2 Bhanu Praveen COBOL Programming 1 Sat Jul 08, 2017 8:58 pm
No new posts Dynamic output file creation in cobol... smileheal COBOL Programming 7 Thu Jun 15, 2017 10:53 pm
No new posts What is the Extract Function in JCL dharmaraok JCL & VSAM 3 Wed Jun 14, 2017 9:12 am
No new posts Search utility(3.14) using JCl and Cobol RKS3 COBOL Programming 12 Sat Jun 10, 2017 10:24 pm
No new posts COBOL DB2 program - zIIP eligible vasanthz COBOL Programming 7 Wed May 31, 2017 5:12 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us