# Thread: Converting binary to decimal

1. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Converting binary to decimal

I wrote a program to turn convert binary input into a decimal number. I'm having trouble finding a problem in the code for it, as it only gives me a solution of 0.

Java Code:
```package binaryConverter;
import java.util.Scanner;

public class binaryConverter {
public static void main(String[] args) {
String binary;
int i = 0;
Scanner input = new Scanner(System.in);
int array[] = {};
int secondArray[] = {128, 64, 32, 16, 8, 4, 2, 1};
int output = 0;

binary = input.nextLine();
String binaryInput[] = binary.split("");
for(i = 0; i < binaryInput.length; i++){
if(binaryInput[i] == "0"){
array[i] = 0;
}else if(binaryInput[i] == "1"){
array[i] = secondArray[i];
}
}
for(int x = 0; x < array.length; x++){
output = output + array[x];
}
System.out.println(output);
}

}```
The code works with multiple arrays. It takes binary input and splits it into an array, each value being 0 or 1. It then goes through the loop looking at those array places. secondArray holds the values for the specific bit of binary (128, 64, 32, so on). If the value of i the loop is on is 3, for example, and the input for that place is a 1, then the value of the third place in the array will be 32. Once all that is done, the second loop goes through and adds each point in the array together to find the final value, which is then displayed. I can't find any noticeable problem in the main code that converts, and I'm 95% sure the problem lies in the second for loop, and they're not adding properly. Any help would be appreciated

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

I haven't looked closely at your code to know if this is the problem. However, you are using == to compare strings ("0" and "1"). You should use equals. However, you might want to use the toCharArray method of String. Then you can compare characters using '0' and '1' with ==.

Regards,
Jim

3. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Thanks for pointing out the need for .equals(), I managed to miss that somehow. I changed it, but it still doesn't work, it still returns 0. I figured out though, that the 0 being returned is the initial value of the output variable, so it tells me that the calculated values aren't being passed into output, or the calculation is always returning 0
Last edited by herpeslurpy; 10-04-2013 at 04:28 PM.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

You are indexing through an array (namely array) which has not been allocated any storage.

Regards,
Jim

5. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Hmmm... I've tried giving the array a specific length, as well as initializing each value, but I still get 0

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

You should repost your code. Also, you should use Arrays.toString() to display your input string array. It may surprise you. And I also suggest you sprinkle println statements throughout your program to see if variables are being updated the way you think they are.

Regards,
Jim

7. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Converting binary to decimal

String binaryInput[] = binary.split("");
Huh? Split on no token at all (empty string) ?

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

It works, sorta. But not the best way. I suggested an alternative approach.

Regards,
Jim

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Converting binary to decimal

Amazing Jim!

Wait, I can target Star Trek jokes at you. A whole new world has just opened up for me.

10. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

Actually, here is a better way. It employs a zero width positive look behind assertion. So it only splits on an empty string if the preceding character is a 1 or 0.
Java Code:
` String binaryInput[] = binary.split("(?<=[01])");`
I still think the OP should have converted to a character array.

Regards,
Jim

11. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

So... Thanks guys, I actually managed to get it working, without changing it more or less, which is weird. Earlier, I start by removing the second for loop and make it calculate the decimal inside the second if statement. After that I added jim's suggestion of using (?<=[01]) to split it, which removed some out of bounds errors. I mean, that fixed it all.
Java Code:
```import java.util.Scanner;

public class binaryConverter {
public static void main(String[] args) {
String binary;
int i = 0;
Scanner input = new Scanner(System.in);
int array[] = new int[8];
int secondArray[] = {128, 64, 32, 16, 8, 4, 2, 1};
int output = 0;

binary = input.nextLine();
String binaryInput[] = binary.split("(?<=[01])");
for(i = 0; i < binaryInput.length; i++){
if(binaryInput[i].equals("0")){
array[i] = 0;
}else if(binaryInput[i].equals("1")){
array[i] = secondArray[i];
output = output + array[i];
}
}
System.out.println(output);
}

}```
Thanks guys

12. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

Originally Posted by herpeslurpy
I mean, that fixed it all.
I don't think so (you did test this, right?). And if this is for a class, then I hope you can readily explain how you are splitting the string. Regular expressions are sort of in the advanced category and your teacher may wonder how you arrived at the solution.

Regards,
Jim
Last edited by jim829; 10-05-2013 at 12:14 AM.

13. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

No, it's not an assignment. I got bored in class today and decided to do it, I'm amazed at how quickly I knocked out most of the code, though. And yes, I tested it thoroughly

14. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

So when you enter 110, what does it return (I am referencing your last posted code)?

Regards,
Jim

15. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Right now I have it set up so it only takes 8 bits of length, but as 00000110, it's 6. I know mah binary

16. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Originally Posted by herpeslurpy
Right now I have it set up so it only takes 8 bits of length, but as 00000110, it's 6. I know mah binary
Well, I guess it can do less, but just 110 will return 192.

17. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,842
Rep Power
10

## Re: Converting binary to decimal

I thought it was free input. So I typed 110 and I got 192. I did not realize you were expecting leading zeroes. Perhaps a bad assumption
on my part.

Regards,
Jim

18. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Oh. I probably should've described it better, either way. But yeah, leading zeros, 8 bit max

19. ## Re: Converting binary to decimal

Not a very flexible or user friendly program.

20. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## Re: Converting binary to decimal

Originally Posted by Junky
Not a very flexible or user friendly program.
Yeah, but I was just bored when I did it, it's not like converting binary is hard in the first place

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•