I intend to write a GUI java program which convert a decimal(base 10) to hexadecimal(base 16). Floating point decimals are also valid input. the code of my program

private void myEnterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              

    float mynum;
    mynum = Float.parseFloat(this.myNumberField.getText());

    Float floatObject = Float.valueOf(mynum);
    myResultField.setText(Float.toHexString(mynum));
    }       

but when I run the program, the input of 12.9 gives 0x1.9cccccp3, whereas I want the output as C.E6666

Help me to solve this problem.

Can you explain the arithmetic you did to get that results?

Can you explain the arithmetic you did to get that results?

No arithmetic, just inbuilt java function/library to convert a decimal number(base 10) with floating point to a a floating point hexadecimal. Isn't there a function like that? I think there is a function for conversion of Integer.

If there is no pre-defined/inbuilt function in java then I have to go through a long process where:

  1. Input as a string (an array of char)
  2. Conversion of string to digits (separate for before and after point as in for 1234.678 to 1234 and 678)
  3. Arithmetic operations of digits to hexadecimal (here separate equivalent for 1234 and 678).
  4. Conversion of digits to string.
  5. Output as string.

Hope you understand what my intentions are!

Can you show how you got C.E6666 from 12.9? I see where the C came from. What about the E6666?

Also can you show what the HEX STRING of 1234.678 would be?

java function/library to convert a floating point to a a floating point hexadecimal.

What is the problem with the Float class's toHexString method?

What i need is a hexadecimal equivalent of a decimal floating number. A decimal 12.9 is equivalent to C.E6666. So help me how to do this.

Pliz explain me by modifying my code. Thnx

decimal 12.9 is equivalent to C.E6666

Please explain how you get that value. This is the third time I have asked this question.

Lets take Decimal no: 123.9
1st integer part : 123

123%16= 11 = B in hex, 123/16= 7
7%16= 7 = 7 in hex
(7/16=0 so stop)
So result is 7B

Now floating part: 0.9
160.9= 14.4, integer part= 14 =E in hex
16
0.4= 6.4, int part= 6 = 6 in hex
16*0.4= 6.4, same 6 in hex
contd upto 5 places
to get E6666
thus the result is 7B.E666

Now floating part: 0.9
160.9= 14.4, integer part= 14 =E in hex
160.4= 6.4, int part= 6 = 6 in hex
16*0.4= 6.4, same 6 in hex
contd upto 5 places
to get E6666

That makes no sense to me. How does 0.9 become E6666? Your explanation leaves out some stuff.
What is this: 160.9 = 14.4??? where did the 160.9 come from? What operation do you do on that amount to get 14.4?

My Mistake, here is the explanation

floating part: 0.9
16 * 0.9= 14.4, integer part= 14 =E in hex
16 * 0.4= 6.4, int part= 6 = 6 in hex
16 * 0.4= 6.4, same 6 in hex
contd upto 5 places
to get E6666
thus the result is 7B.E666

I am very sorry for the trouble. Actually I used a nokia mobile last time so....
Anyways hope I have explain this time.

I think I see now. The digits to the right of the decimal point are 1/16^x where x goes from 1 to n as the digits go to the right.
Converting E666 to decimal would give: 14/16 + 6/256 + 6/4096 + 6/65536 = 0.899993896484375

If you have the algorithm for converting a float to that format, what is your question? Where are you having problems writing the code?

the thing is I thought there is an inbuilt java function/method to convert a floating point decimal number to equivalent floating point hexadecimal number.
Like when I enter 123.9 as decimal then the output would be 7B.E6666 (using inbuilt java function in a line).
I hate to say but I am looking for only inbuilt function.

Sorry,I don't know of any such methods in java SE.

Do you see why the result is 0x1.9cccccp3?

I don't think you can do any better than that with the standard library. Converting fractions into other bases is not a very common need (unlike integers, which are quite handy to have in base-N sometimes.)

What you might do with a minimal amount of coding is take the fractional part, multiply it by 16^N where N is the number of hexadecimal places you want, convert it to an integer, and output that in hex -- something like this (untested):

System.out.printf("%x.%x\n", (int)x, (int)((x - int(x))*0x1000000))

Good luck

Ok I'll try with basics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.