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:
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 } } } }