I'm working on this program that takes a binary string and converts it to decimal, using this guide to convert from binary to decimal. When I run through the for loop in my head, I get what the correct outputs would be. And yet, when I run my program, I get this strange output:
1
3
7
15
31
63
127
The actual output should look like this:
1
2
5
11
22
44
89
I cannot figure this out for the life of me. Why is my program doing this? Here's the current source code:
public class BinaryToDecimal
{
public static void main(String[] args)
{
String binary = "1011001";
int toMultiplyBy;
int decimalValue = 0;
for (int i = 1; i <= binary.length(); i++)
{
int whatNumber = binary.indexOf(i);
if (whatNumber == 0)
{
toMultiplyBy = 0;
}
else
{
toMultiplyBy = 1;
}
decimalValue = ((decimalValue * 2) + toMultiplyBy);
System.out.println(decimalValue);
}
}
}
Strings are 0 based so you should loop through the String from 0 to < the String's length, but indexOf(...)
, is not what you want to use since this will search for the location in the String of small ints which makes no sense. You don't care where the char equivalent of 2 is located in the String or even if it is in the String at all.
Instead you want to use charAt(...)
or subString(...)
and then parse to int. I would use
for (int i = 0; i < binary.length(); i++) {
int whatNumber = charAt(i) - '0'; // converts a numeric char into it's int
//...
To see what this is doing, create and run:
public class CheckChars {
public static void main(String[] args) {
String foo = "0123456789";
for (int i = 0; i < foo.length(); i++) {
char myChar = foo.charAt(i);
int actualIntHeld = (int) myChar;
int numberIWant = actualIntHeld - '0';
System.out.printf("'%s' - '0' is the same as %d - %d = %d%n",
myChar, actualIntHeld, (int)'0', numberIWant);
}
}
}
Which returns:
'0' - '0' is the same as 48 - 48 = 0
'1' - '0' is the same as 49 - 48 = 1
'2' - '0' is the same as 50 - 48 = 2
'3' - '0' is the same as 51 - 48 = 3
'4' - '0' is the same as 52 - 48 = 4
'5' - '0' is the same as 53 - 48 = 5
'6' - '0' is the same as 54 - 48 = 6
'7' - '0' is the same as 55 - 48 = 7
'8' - '0' is the same as 56 - 48 = 8
'9' - '0' is the same as 57 - 48 = 9
The numbers that represent the chars is based on the old ASCII table that gave each symbol a numeric representation. For more on this, please look here: ASCII Table
Two points:
indexOf()
with substring()
. In your case, binary.indexOf(i)
does the following. First, the integer i
is converted to a string. Then binary
is searched left-to-right for a substring matching the string value of i
. The first time through the loop i==1
. This returns zero, because there's a 1
at index zero in binary
. The second time, the value of i
is 2
. There's no 2
in binary
, so this returns zero. For i==3
, you're looking for a string 3
in binary
, which will never be true.Take a look at the String#substring()
method, which is what I believe you intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With