473,382 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Decimal to Binary Conversion program

realin
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.  
  12.     try{nNum=Integer.parseInt(inp.readLine());}
  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.  
please help me
thanks
Sep 7 '07 #1
14 26785
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 :)
Use BufferedReader or Scanner instead of DataInputStream for reading input.
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.  *
  10.  * @author Admin
  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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Raja | last post by:
IS there any inbuilt decimal to binary conversion function in C++
4
by: nyy | last post by:
Hello everybody on this group. I have this program that is supposed to display a temp. conversion from fahrenheit to celsius, can anybody kindly can tell me what is wrong? Thanks. <!DOCTYPE html...
16
by: Mars | last post by:
I'm writing a program for listing all binary numbers of the same length with the same number of 1s. e.g. 0011 0101 0110 1001 1010 1100
3
by: allanarendra_reddy | last post by:
hi plz send me the logic for the binary convertio program
21
by: AsheeG87 | last post by:
Hey Everyone~ I'm still a C++ Rookie so please bear with me on this. I'm doing a temperature conversion program with prototype functions. Basicly, I was wondering if some of you would take a look...
1
by: farahomer | last post by:
Hi !! I have a question. I am developing a code in C#.NET. So here I have a long string in this format: 000000000010 000000100100 (for example). Its 24 digit long, split into 12 digits by a space in...
4
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
1
by: Don Hillgen | last post by:
I need to expand a field in a db2 table, must I write a conversion program???
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.