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

Thread: Unexpected output java

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Unexpected output java

    I am trying to write a program for converting positive binary inputs into hex. in the hex output the point (".")is missing.

    Suppose my expected output is e7.6 , but i am getting e76.

    only the "." is missing.

    here is my BinToHex class..

    import java.io.*;
     
     
    public class BinToHex {
        double tempDec,fractionpart;
        long longofintpart,templongDec;
        String inpu ="11100111.011";
        String hexOutput=null,tempDecString,hex = null;
     
        static int i = 1;
     
        public void convertbintohex() {
     
                if (inpu.contains(".")) {
                    int placesAfterPoint = inpu.length() - inpu.indexOf(".") - 1;//every thing
                    long numerator = Long.parseLong(inpu.replace(".", ""), 2);//goes 
                    double decimalOfInput = ((double) numerator) / (1L << placesAfterPoint);//alright  till here 
     
     
                    while (true) {
                        tempDec = decimalOfInput * 16;
                        if (tempDec == (int)tempDec) {
                            tempDecString = String.valueOf((long)tempDec);
                            templongDec = Long.parseLong(tempDecString, 10);
                            hexOutput = Long.toHexString(templongDec);
     
                            break;
                        } else {
                            longofintpart  = (long)tempDec;
                            hex=Long.toHexString(longofintpart);
                            if(i==1){
                                hexOutput = hex + ".";
                                i=i+1;
                            }else{
                                hexOutput = hexOutput + hex;
                            }
                            fractionpart = tempDec-(int)tempDec;
                            decimalOfInput = fractionpart;
                        }
                    }
                } else {
                        // this part is ok
                    tempDecString = String.valueOf(Integer.parseInt(inpu, 2));
                    templongDec = Long.parseLong(tempDecString, 10);
                    hexOutput = Long.toHexString(templongDec);
                }
                System.out.println(hexOutput);
        }   
    }
    my main Test class..
    public class Test{
        public static void main(String args[]){
            BinToHex i = new BinToHex();
            i.convertbintohex();    
        }
    }

    here is how decimal fraction is converted into hex
    or here too
    plz help me.
    Last edited by Tiash; October 1st, 2014 at 12:27 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Unexpected output java

    what is a "." doing in a hex number? I've never seen that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Unexpected output java

    Quote Originally Posted by Norm View Post
    what is a "." doing in a hex number? I've never seen that.
    if there can't be "." in hex then how is a fraction represent in hex. indeed i've seen "." in hex several times.

    fraction can be presented in hex
    Last edited by Tiash; October 1st, 2014 at 12:29 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Unexpected output java

    I've never seen any non-decimal number with a point.
    I guess its allowed.

    What is the algorithm for creating the digits to the right of the point?

    Can you show the math needed to do the conversion?
    Also provide an example.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Unexpected output java


  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Norm (September 30th, 2014)

  7. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Unexpected output java

    if there can't be "." in hex then how is a fraction represent in hex. indeed i've seen "." in hex several times.

    fraction can be presented in hex

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Unexpected output java

    What are the steps your program is doing to solve the problem.
    For the digits to the left of the point it would do something like this:
    convert base 2 digits to int
    convert int to base 16 digits

    What would it do for the digits to the right of the point?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  2. [SOLVED] Unexpected type
    By hooshdar3 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 17th, 2014, 09:14 AM
  3. unexpected output
    By sumitroy in forum Threads
    Replies: 4
    Last Post: June 30th, 2014, 07:30 AM
  4. Unexpected Output Format
    By playinmyblues in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 06:56 PM
  5. Unexpected ArrayOutOfBoundsError
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2010, 04:00 AM

Tags for this Thread