Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Making Binary Converter script from scratch, running into math issue.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by mwebb; November 8th, 2011 at 05:33 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; November 8th, 2011 at 05:50 PM.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  9. The Following User Says Thank You to Norm For This Useful Post:

    mwebb (November 8th, 2011)

  10. #9
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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!
    Last edited by mwebb; November 8th, 2011 at 07:10 PM.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

Similar Threads

  1. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM
  2. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  3. PRoblem Running Executable script in linux
    By ntu in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: May 2nd, 2011, 06:06 AM
  4. Making a little Lua script 'generator', JFrame showing up blank.
    By scribbleno1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 2nd, 2010, 07:00 PM
  5. Piglatin Converter
    By jross21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:09 PM