-1

I would like to convert a decimal number into a binary number and return that binary number. However, I need to be able to represent this binary number as one whole int variable. The examples and past questions I've seen only returns 0's and 1's separately, which won't work.

Right now, the way I'm doing it I'm storing the 0's and 1's in an int array. Is there any way to get all these array elements and form one int variable? Or is there another, better way? I'm trying to make as few java library calls as possible (ie few parseInt(), etc.)

yiwei
  • 4,022
  • 9
  • 36
  • 54
  • 1
    Possible duplicate of http://stackoverflow.com/questions/5203974/converting-decimal-to-binary-in-java – Rohan Kumar Feb 05 '13 at 04:33
  • So, are you trying to convert an integer into a boolean array and back? – EAKAE Feb 05 '13 at 04:33
  • @RohanKumar that example returns a `String` object, I need it to return an `int` variable – yiwei Feb 05 '13 at 04:35
  • 2
    from your point of view an int is an int, it's a number. binary, decimal, hex is a mean of representation. there are no binary ints, or decimal ints – Denis Tulskiy Feb 05 '13 at 04:55
  • Your question is unclear. If you are given the decimal number, say, 42, is the result you want an int containing the decimal number `101010`? – Jim Garrison Feb 05 '13 at 04:55
  • @JimGarrison yes that is exactly what i mean...sorry for the vagueness – yiwei Feb 05 '13 at 04:56
  • 2
    That does not seem to make any sense. The largest number you could represent would be 1023. I think you need to explain WHY you want to do this. – Jim Garrison Feb 05 '13 at 04:57
  • 2
    I don't think you quite understand what "decimal" or "binary" means, or what an integer is. – Brian Roach Feb 05 '13 at 04:59
  • @JimGarrison: 524287 if he uses `long`, that's something already! – Denis Tulskiy Feb 05 '13 at 05:03
  • I find it impossible to believe that you only found examples that returned 0 and 1, which isn't an example of converting decimal to binary at all. – user207421 Feb 05 '13 at 06:57

3 Answers3

-1

Try the code

import java.util.*;

public class number
{
    public static void main (String [] args)
    {
        Scanner input = new Scanner (System.in);
        System.out.println ("Input decimal number");
        int decimal = input.nextInt ();
        input.close ();

        int base = 2;
        int result = 0;
        int multiplier = 1;

        while (decimal>0)
        {
            int residue = decimal%base;
            decimal = decimal/base;
            result = result +residue*multiplier;
            multiplier = multiplier * 10;
        }
        System.out.println ("binary....."+result);
    }
}

For more info http://forum.codecall.net/topic/54004-decimal-to-binary-number/

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
-1

Try this code:

import java.lang.*;
import java.io.*;
public class BinaryToDecimal{
    public static void main(String[] args) throws IOException{
        BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the Binary value: ");
        String str = bf.readLine();
        long num = Long.parseLong(str);
        long rem;
        while(num > 0){
            rem = num % 10;
            num = num / 10;
            if(rem != 0 && rem != 1){
                System.out.println("This is not a binary number.");
                System.out.println("Please try once again.");
                System.exit(0);
            }
        }
        int i= Integer.parseInt(str,2);
        System.out.println("Decimal:="+ i);
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Satyam
  • 1,672
  • 3
  • 20
  • 34
-1

Tested Code.........

private Double decToBin(int nm) {
    String hex = "" + nm;
       int i = Integer.parseInt(hex);
    String by = Integer.toBinaryString(i);
    System.out.println("Binary: " + by);
    return Double.parseDouble(by);
}

Write Where you have need...??

else if (re.equals(" Bin ")) {
                    try {

                        String prev = data.get(i - 1);
                        result =decToBin(Integer.parseInt(prev));
                        re = "" + result;
                        i++;
                        twoValue = true;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }}
kk yadav
  • 45
  • 1
  • 7