# Converting binary to decimal

• 10-04-2013, 04:53 PM
herpeslurpy
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.

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
• 10-04-2013, 05:15 PM
jim829
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
• 10-04-2013, 05:22 PM
herpeslurpy
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
• 10-04-2013, 05:30 PM
jim829
Re: Converting binary to decimal
You are indexing through an array (namely array) which has not been allocated any storage.

Regards,
Jim
• 10-04-2013, 05:41 PM
herpeslurpy
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
• 10-04-2013, 05:46 PM
jim829
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
• 10-04-2013, 05:50 PM
gimbal2
Re: Converting binary to decimal
Quote:

String binaryInput[] = binary.split("");
Huh? Split on no token at all (empty string) ?
• 10-04-2013, 05:53 PM
jim829
Re: Converting binary to decimal
It works, sorta. But not the best way. I suggested an alternative approach.

Regards,
Jim
• 10-04-2013, 05:59 PM
gimbal2
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-04-2013, 06:07 PM
jim829
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.
Code:

` String binaryInput[] = binary.split("(?<=[01])");`
I still think the OP should have converted to a character array.

Regards,
Jim
• 10-04-2013, 11:25 PM
herpeslurpy
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.
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
• 10-05-2013, 12:53 AM
jim829
Re: Converting binary to decimal
Quote:

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
• 10-05-2013, 01:58 AM
herpeslurpy
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
• 10-05-2013, 02:00 AM
jim829
Re: Converting binary to decimal
So when you enter 110, what does it return (I am referencing your last posted code)?

Regards,
Jim
• 10-05-2013, 02:32 AM
herpeslurpy
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
• 10-05-2013, 02:34 AM
herpeslurpy
Re: Converting binary to decimal
Quote:

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.
• 10-05-2013, 02:37 AM
jim829
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
• 10-05-2013, 06:30 PM
herpeslurpy
Re: Converting binary to decimal
Oh. I probably should've described it better, either way. But yeah, leading zeros, 8 bit max
• 10-06-2013, 05:31 AM
Junky
Re: Converting binary to decimal
Not a very flexible or user friendly program.
• 10-06-2013, 07:56 PM
herpeslurpy
Re: Converting binary to decimal
Quote:

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