View previous topic :: View next topic
|
Author |
Message |
avaneendra_linga
New User
Joined: 13 Dec 2006 Posts: 73 Location: Hyderabad
|
|
|
|
HI,
IS THERE ANY FUNCTION TO CONVERT DECIMAL VALUE INTO BINARY IN
COBOL? |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
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 |
|
|
avaneendra_linga
New User
Joined: 13 Dec 2006 Posts: 73 Location: Hyderabad
|
|
|
|
this i understood William ........I NEED TO DISPLAY THE DECIMAL VALUE IN BINARY FORMAT....... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
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 |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
NOTE:
Above program will work only in range 0 to 65535 of variable NUMER1. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
Back to top |
|
|
sarathmuruga Currently Banned New User
Joined: 04 Jan 2007 Posts: 1 Location: Kerala
|
|
|
|
[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 |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
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. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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. . . . |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
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.... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Well, it is binary. . . .
Like i posted earlier, if displaying the ones and zeros was the goal - very good catch |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
I do agree, he caught it well before I did....
Pending feedback from avaneendra_linga, good job Abhijit! |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
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 |
|
|
avaneendra_linga
New User
Joined: 13 Dec 2006 Posts: 73 Location: Hyderabad
|
|
|
|
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 |
|
|
|