Results 1 to 20 of 20
Thread: Converting binary to decimal
- 10-04-2013, 04:53 PM #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); } }
I am not a clever man
- 10-04-2013, 05:15 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 05:22 PM #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 05:28 PM.
I am not a clever man
- 10-04-2013, 05:30 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Converting binary to decimal
You are indexing through an array (namely array) which has not been allocated any storage.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 05:41 PM #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
I am not a clever man
- 10-04-2013, 05:46 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 05:50 PM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Converting binary to decimal
String binaryInput[] = binary.split("");"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 10-04-2013, 05:53 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Converting binary to decimal
It works, sorta. But not the best way. I suggested an alternative approach.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 05:59 PM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 10-04-2013, 06:07 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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])");
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 11:25 PM #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); } }
I am not a clever man
- 10-05-2013, 12:53 AM #12
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Converting binary to decimal
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,
JimLast edited by jim829; 10-05-2013 at 01:14 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-05-2013, 01:58 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
I am not a clever man
- 10-05-2013, 02:00 AM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Converting binary to decimal
So when you enter 110, what does it return (I am referencing your last posted code)?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-05-2013, 02:32 AM #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
I am not a clever man
- 10-05-2013, 02:34 AM #16
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
- 10-05-2013, 02:37 AM #17
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-05-2013, 06:30 PM #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
I am not a clever man
- 10-06-2013, 05:31 AM #19
Re: Converting binary to decimal
Not a very flexible or user friendly program.
- 10-06-2013, 07:56 PM #20
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Converting binary to decimal using recursion
By _inase in forum New To JavaReplies: 4Last Post: 10-14-2011, 03:31 AM -
anyone know's how to program a conversion of binary-decimal , decimal-binary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08-25-2011, 08:32 PM -
Converting characters to decimal to binary
By Majeh in forum New To JavaReplies: 4Last Post: 02-05-2011, 12:06 AM -
Algorithm for converting binary/hex to decimal
By addictz04 in forum New To JavaReplies: 2Last Post: 11-29-2010, 07:49 PM -
converting decimal to binary value using recursion in java
By Anindo in forum New To JavaReplies: 3Last Post: 07-25-2009, 02:44 PM
Bookmarks