# decimal to binary problem

• December 26th, 2010, 01:53 AM
mick.bhattarai
decimal to binary problem
my code:

Code java:

```    public class d2b {   private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int[16], convnum, alter = 1;   d2b(int decpass){ //decpass is passed from any other class or main as an integer. decnum = decpass; }   public void convert(){     while(decnum > 1){   modnum = decnum % 2; quotnum = decnum / 2;   decnum = quotnum;   binarray[count] = modnum;   count++;   }   binarray[count] = decnum;     while(count >= 0){   if(alter == 1){   convnum = 1; alter++;   } else{   if(binarray[count] == 1){ convnum = (convnum * 10) + 1; } else{ convnum = convnum * 10; }   }         count--;   }   System.out.printf("%d", convnum);   }   }```

What it tries to do:

I wrote this program as my assignment. I came up with the logic(finally *phew*) for a program to convert decimal to binary and store it as an integer. I tried the algorithm without the arrays but couldn't solve it at all(maybe i am dumb :P), so i used arrays and successfully was able to complete this task within minutes. however, now i can only convert the values upto certain numbers....ex, if i pass 999, it will convert the value as 1111100111 but if i pass 9999 it will show -468765865. i have a hunch, that, its because i have to do it as 'long int' data type, am i right? if not where am i going wrong? also when i derived the algo for it i saw that it wouldn't need more than 15 array size or am i wrong there too?

• December 26th, 2010, 02:55 AM
JavaHater
Re: decimal to binary problem
why only size of 16 in binarray[] = new int[16] ??
• December 26th, 2010, 03:03 AM
mick.bhattarai
Re: decimal to binary problem

i assigned only 16 array size because for now i just want to check the conversion till 4 digit decimal (i.e. 9999) which should ultimately produces a 15 digit binary(but that is not happening as you can read from my above post) so i gave the array size a 16.
• December 26th, 2010, 06:48 AM
JavaHater
Re: decimal to binary problem
i don't know what you doing with the 2nd whle loop, but i can say it unnecessary since you are already doing the divide by 2 method.
Code :

```public class d2b { private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int[16], convnum, alter = 1; d2b(int decpass){ //decpass is passed from any other class or main as an integer. decnum = decpass; } public void convert(){ while(decnum > 1){ decnum = decnum / 2; binarray[count] = decnum % 2; count++; } for(int i= binarray.length-1 ; i>=0; i--){ System.out.printf("%d", binarray[i] ); } System.out.printf("0\n"); } public static void main(String[] args){ d2b num = new d2b(9999); num.convert(); } }```
• December 26th, 2010, 12:00 PM
mick.bhattarai
Re: decimal to binary problem
You do realize that i am trying to store the binary as an integer(even though it seems to be the wrong way to solve this problem), don't you? so the "System.out." method(which seems more like the way this problem should be handled) is just for printing while, i am trying to store it into an integer variable. However i could've used for loop instead i choose while loop so it doesn't matter, i used while because of its ease and control i can get. But the first if() after the while loop was needed(for me, because i dont know any other way) to rectify the problem with the first array(from the last) value being used as it will always be 1, ex 100. when the modulus way is being done binarray[0] = 0, binarray[1] = 0, binarray[2] = 1. So as you can see binarray[2] or any other last binarray will always have value 1 so this created a problem for me. As for the second if()-else, its to now combine those single 0/1 digits into a number. Again i was doing this method to store binary into an integer, if i just wanted to print it , i could have(without even using array). None the less, thank you for your input. As for the problem, it still insists.

EDIT: found the solution.

The solution is to use long instead of int.
• December 26th, 2010, 06:19 PM
JavaHater
Re: decimal to binary problem
try setting the size of your binarry dynamically. for example binarray = new int[ decnum / 2 ] , instead of hard coding 16 as the size.
• December 26th, 2010, 06:48 PM
mick.bhattarai
Re: decimal to binary problem
i dont think initializing binarray dynamically is going to help me with this program, specially [decnum / 2] because the array size is pretty much the number of integers a converted binary will have but thank you for your input. However problem is solved.