Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: decimal to binary problem

1. ## decimal to binary problem

my code:

```

public class d2b {

private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int, 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 ), 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?

Please any input would be helpful. Thank you.  Reply With Quote

2. ## Re: decimal to binary problem

why only size of 16 in binarray[] = new int ??  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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.
```public class d2b {
private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int, 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();
}
}```  Reply With Quote

5. ## The Following User Says Thank You to JavaHater For This Useful Post:

mick.bhattarai (December 26th, 2010)

6. ## 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, binarray = 0, binarray = 1. So as you can see binarray 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.  Reply With Quote

7. ## 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.  Reply With Quote

8. ## 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.  Reply With Quote