471,829 Members | 1,884 Online

# Decimal to Binary Conversion program

254 100+
hi guys

i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011

now how do i swap it off..

here is teh code

Expand|Select|Wrap|Line Numbers
1. import java.io.DataInputStream;
2. class dec2b{
3.
4. public static void main(String a[]){
5.
6.     DataInputStream inp=new DataInputStream(System.in);
7.     int nNum=0,nTemp=0,nSum=0,nBin=0,i=0;
8.
9.
10.     System.out.println("Enter a Number to Convert into Binary");
11.
13.     catch(Exception e){}
14.
15.     while(nNum!=0)
16.     {
17.
18.         nTemp=nNum;
19.
20.         if(nTemp%2==1)
21.         nBin=1;
22.         if(nTemp%2==0)
23.         nBin=0;
24.
25.         nSum=nSum*10+nBin;
26.         nNum=nNum/2;
27.
28.     }
29.     System.out.println(nSum);
30. }}
31.
thanks
Sep 7 '07 #1
14 26632
JosAH
11,448 Expert 8TB
hi guys

i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011

now how do i swap it off..
You don't; you should read the API documentation for the Integer class. It's all there.

kind regards,

Jos
Sep 7 '07 #2
realin
254 100+
You don't; you should read the API documentation for the Integer class. It's all there.

kind regards,

Jos
hi jos,

I know there is a straight function integer(11,2) .. but i wanna make it thru logic..

cause that is my assignment.. thanks :)
Sep 7 '07 #3
JosAH
11,448 Expert 8TB
hi jos,

I know there is a straight function integer(11,2) .. but i wanna make it thru logic..

cause that is my assignment.. thanks :)
Recursion is your friend then (it avoids reversing the entire thing at the end).

Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).

kind regards,

Jos
Sep 7 '07 #4
realin
254 100+
Recursion is your friend then (it avoids reversing the entire thing at the end).

Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).

kind regards,

Jos

thanks seems interesting.. will try :)

thanks thanks a lot
Sep 7 '07 #5
r035198x
13,262 8TB
hi jos,

I know there is a straight function integer(11,2) .. but i wanna make it thru logic..

cause that is my assignment.. thanks :)
To reverse an integer's digits, just make it a String, pass it to a StringBuilder and call the reverse method on it.
Sep 7 '07 #6
r035198x
13,262 8TB
Recursion is your friend then (it avoids reversing the entire thing at the end).

Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).

kind regards,

Jos
There you go again ...
Sep 7 '07 #7
hello..can you tell me how to loop the decimal to binary conversion?
Aug 6 '08 #8
JosAH
11,448 Expert 8TB
hello..can you tell me how to loop the decimal to binary conversion?
What do you mean by "how to loop"? For all the rest: read this thread.

kind regards,

Jos
Aug 6 '08 #9
Expand|Select|Wrap|Line Numbers
1. /*
2.  * To change this template, choose Tools | Templates
3.  * and open the template in the editor.
4.  */
5.
6. package decimalbinary;
7. import javax.swing.*;
8. /**
9.  *
11.  */
12. public class Main {
13.
14.     /**
15.      * @param args the command line arguments
16.      */
17.     public static void main(String[] args) {
18.         // TODO code application logic here
19.
20.         int x,i,q,z,temp=0;
21.         q=0; z=0;
22.        String a="";
23. x =Integer.parseInt(JOptionPane.showInputDialog("Please enter your number"));
24.
25.     while (x!=0)
26.     {temp=x;
27.     x=temp/2;
28.      q=temp%2;
29.      a=q+a;
30.
31.     }
32.     String msg="Output is "+a;
33.     JOptionPane.showMessageDialog(null, msg);
34.     }
35.
36. }
Jan 15 '10 #10
pbrockway2
151 Expert 100+
@jesz143
Accumulating the digits in a StringBuilder and then reversing it at the end (as suggested by r\d*x) would remove the need for all these string concatenations.

Is there a reason why this thread was woken from its well deserved rest?
Jan 15 '10 #11
Expand|Select|Wrap|Line Numbers
1. class Binary
2. {
3.     public static void main(String args[])
4.     {
5.     int result=0;
6.     int val=48,nbin=0,k=0;
7.     while(val!=0)
8.         {
9.
10.             nbin=(val%2==0)?0:1;
11.                 result=nbin*(int)Math.pow(10,k)+result;
12.                 val/=2;
13.         ++k;
14.
15.         }
16.     System.out.println(result);
17.     }
18. }
19.
I hope this may work for you
Apr 18 '10 #12
@JosAH
Hello JosAH,
I posted my solution for decimal to binary conversion.Since am newbie I need a suggestion to modify my solution
Apr 20 '10 #13
jkmyoung
2,057 Expert 2GB
pbrockway2: I like your solution. No need for recursion!
Modifying the original code a little:
Expand|Select|Wrap|Line Numbers
1. StringBuffer sb = new StringBuffer();
2.   while(nNum!=0)  // code changes nNum
3.     {
4.         if(nNum%2==1)
5.           sb.append(1);
6.         else //        if(nNum%2==0)
7.           sb.append(0);
8.         nNum >>= 1;  // same as nNum /= 2;
9.     }
10. sb.reverse();
11.
Apr 20 '10 #14
This is the Program Which will convert a Decimal no to Binary fie ..

Expand|Select|Wrap|Line Numbers
1.
2. class Binary{
3.     public static void main(String[] args){
4.         int no=150;       //Input the No here
5.         int i=0;
6.         int size=no/2;     //the no of 0 and 1 is no/2 approximetly;
7.         int a[]=new int[++size];    //creating an array of that size
8.         while(no>0){
9.
10.             a[i++]=(no%2==0)?0:1;
11.         no /=2;
12.         }
13.
14.         for(int j=i-1;j>=0;j--){
15.
16.             System.out.print(a[j]);
17.
18.         }
19.
20.     }
21. }
22.
23.
24.
May 6 '13 #15