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

Thread: Write a program that converts floating-point numbers to/from decimal representation to Hex/Binary representations.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a program that converts floating-point numbers to/from decimal representation to Hex/Binary representations.

    I am a writing a program to converts floating-point numbers to/from decimal representation to Hex/Binary representations. We have to do a setup which I already did, and now I have to figure out how to convert. I started out by trying to convert from hex to float first. I only got this far:
                System.out.println("Enter Hex you want to convert");
                String hex = input.nextLine();
                int i = Integer.parseInt(hex);
                String by = Integer.toBinaryString(i);
                System.out.println("This is Binary: " + by);
                String expo = by.substring(0, exponent);
                String frac = by.substring(exponent+1);
    My problem is that the toBinaryString deletes leading 0's, so that my sign-bit and other leading zeros are lost. I also don't really know how to move on from this. Is there any way to convert hex to binary without loosing the leading zeros?

    I am sorry if I posted this in the wrong place, or the wrong way, this is my first post here.


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a program that converts floating-point numbers to/from decimal representation to Hex/Binary representations.

    Same poster again, I figured out how to go from hex to decimal floating point, but I can't go the opposite way. How do I go from decimal 8.5 to a hex representation of that floating point in java? I know how to do it on paper, but I can't figure out how to code it. 8.5 is 1000.1 in binary, how do I code that in java? And also, how do I move on from that?

                System.out.println("Enter Float you want to convert: ");
                String f = input.nextLine();
                double de = Double.parseDouble(f);
                String binFl = Long.toBinaryString(Double.doubleToRawLongBits(de));
                System.out.println(binFl);
    I feel like what I am doing is completely wrong. I can't figure out how to go from a double to binary.

Similar Threads

  1. Replies: 26
    Last Post: December 7th, 2012, 08:58 AM
  2. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 11:18 AM
  3. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  4. using Eclipse and every program gives the errorr with floating numbers
    By Neera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2010, 12:11 PM
  5. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM