IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Function for converting DECIMAL to BINARY in Cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
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
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: 3156
Location: Tucson AZ

PostPosted: Thu Mar 08, 2007 4:36 pm
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
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

Moderator Emeritus


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

PostPosted: Thu Mar 08, 2007 9:32 pm
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: 691
Location: Earth

PostPosted: Thu Mar 08, 2007 9:51 pm
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: 691
Location: Earth

PostPosted: Thu Mar 08, 2007 9:53 pm
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

Moderator Emeritus


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

PostPosted: Thu Mar 08, 2007 9:58 pm
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
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: 691
Location: Earth

PostPosted: Fri Mar 09, 2007 11:27 am
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

Moderator Emeritus


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

PostPosted: Sat Mar 10, 2007 12:11 am
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: 3156
Location: Tucson AZ

PostPosted: Sat Mar 10, 2007 2:57 am
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

Moderator Emeritus


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

PostPosted: Sat Mar 10, 2007 4:25 am
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: 3156
Location: Tucson AZ

PostPosted: Sat Mar 10, 2007 4:30 am
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top