Making Binary Converter script from scratch, running into math issue.
I understand there are more simplified ways of doing what I am making now, but for the time being I am just using my current knowledge of Java to expand on what I already know. So right now I am making a script to convert between binary and text and vice versa. One issue I am having is that the decimal answer is coming out all wrong when I test the binary to text (decimal for now) part. I keep getting this 4-5 digit answer whenever I enter a 8-bit binary number. I've rechecked my code and broke it down to parts, printed each value through the loop but for some reason I get these bizarre numbers. Here's what I have, but keep in mind that THIS IS INCOMPLETE:
Code :
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class BinaryOrText
{
public static void main(String[] args) throws IOException
{
int method;
int iter = 0;
int binary;
int power = 0;
int decimal;
char binaryDigit = 0;
String strBinary;
String text = "";
Scanner in = new Scanner(System.in);
System.out.print("Pick a Conversion Method; Enter [1] to Binary | [2] to Text: ");
method = in.nextInt();
//text to binary
if (method == 1){
System.out.print("Enter text: ");
text = in.next();
char charT = text.charAt(0);
System.out.println((int)charT);
// ...Incomplete
}
//binary to text
else if (method == 2){
decimal = 0; //declare
System.out.print("Enter Binary: "); //prompt
binary = in.nextInt(); //assign original input
strBinary = Integer.toString(binary); //make a string version
power = strBinary.length() - 1; //sets the starting power by length of binary code
for (int i = 0; i < strBinary.length(); i++){
binaryDigit = strBinary.charAt(iter); //Takes binary digit from char position.
decimal += binaryDigit*Math.pow(2,power); // digit * 2^pow.
System.out.println(decimal);
iter++; //Goes to next binary digit.
power--; //Goes to power on next digit
}
}
}
}
Re: Making Binary Converter script from scratch, running into math issue.
Can you show the program's input and output?
Add some comments to the output showing what you want it to be.
Re: Making Binary Converter script from scratch, running into math issue.
(// comments I added afterwards)
Output:
Pick a Conversion Method; Enter [1] to Binary | [2] to Text: 2 //Method 2 for binary to text.
Enter Binary: 10110110 //a binary string I entered.
6272 // As you can see the starting number of what is supposed to be the result of 1*2^7 is showing up as 6272.
9344 // All the other numbers down are the new added result after going to the next digit following the formula...
10912 // (next Binary digit)*2^(1 less exponent)
11696 // In this case it should go (1*2^7)+(0*2^6)+(1*2^5)+(1*2^4)+(0*2^3)+(1*2^2)+(1 *2^1)+(1*2^0)
12080 //
12276 //
12374 //
12422 // < And the final result should be 182 by that formula with the binary I entered, but my output says otherwise.
Re: Making Binary Converter script from scratch, running into math issue.
You did not show what the input you entered for the program.
Why do you read the input as int vs as String?
Re: Making Binary Converter script from scratch, running into math issue.
No I showed the input, here its in bold. I'm not sure if I know what you mean. I accepted it as an int because I needed to do some math with it. But then I made a String copy of it so that I can make an equation to determine the starting power by the length of it and to get chars from it. Then I used the power and binaryDigit variables to help bring the answer to the variable known as decimal.
Re: Making Binary Converter script from scratch, running into math issue.
Did you enter a 1 or a 2 also????
You need to simplify the input for testing. Change the input to one digit: 1
Add printlns with labels that say what is being printed to the code to show the values of the all variables as they are computed. Don't just print out a naked variable without a label.
Re: Making Binary Converter script from scratch, running into math issue.
I entered a 2 as I have bold-ed above for binary to text method. But I think I have figured out that, when I re-assign and add the chars from the binaryDigit variable, it translates it to its ASCII number first then assigns it. I figure this because I just made the equation, decimal += binaryDigit; and the first result in the output was 49, then it kept adding up to larger numbers until it hit 340. If this is the case, would you know how to make the decimal variable accept the char as is instead of taking its ASCII value?
Re: Making Binary Converter script from scratch, running into math issue.
You need to convert/parse the char value if you want to convert a '1' to a 1.
A quick and dirty way to do that is to subtract '0' from it.
'1' - '0' = 1
Re: Making Binary Converter script from scratch, running into math issue.
Strange, when I try that it gives me the possible loss of precision error, required: char found: int pointing to '0' when I try binaryDigit = binaryDigit - '0' even though both are chars.?
EDIT: But when I try binaryDigit -= '0' it works. Really strange but hey, looks like my output is coming out right. So, awesome and thanks!
Re: Making Binary Converter script from scratch, running into math issue.
What you see is not what you get.
The problem with printing out '1' and 1 is that they print out the same. But your code was using the char in an formula that showed you that '1' was not 1. This is always a confusion for beginners.
You were quick to see it and now know about it and should not have trouble with it later.