-1

I've searched a decent amount but no answer on converting a hexadecimal string (that's not a whole number) such as 0x11.11 or 0x1p3 to its decimal form in Java (float or double) without doing it manually. Any answer?

Riftus
  • 394
  • 4
  • 14
  • do you enter them that way into source code like `double d = 0x11.11;` or in what format do you get them and what format do you need them in? – zapl Jan 18 '14 at 04:08
  • sorry, I should have clarified in the OP that I'm reading the hex values in as strings – Riftus Jan 18 '14 at 04:16
  • have you tried whether `Double.parseDouble(String)` happens to support that format? – zapl Jan 18 '14 at 04:17
  • possible duplicate of [How to convert hex string to float in Java?](http://stackoverflow.com/questions/1071904/how-to-convert-hex-string-to-float-in-java) – Jim Garrison Jan 18 '14 at 04:19
  • @JimGarrison this post draws from the same concept, but I'm still unsure of how to deal with an actual floating point value in hexadecimal form such as 0x11.11 from that post's example and from reading over the intBitsToFloat documentation – Riftus Jan 18 '14 at 04:36
  • This question is about a hex representation of the value of the float. [How to convert hex string to float in Java?](https://stackoverflow.com/questions/1071904/how-to-convert-hex-string-to-float-in-java) was about a hex representation of a float's bit pattern. It's the difference between representing 32 bit floating point 1 as 0x1p0 and 0x3f800000. – Patricia Shanahan Jan 18 '14 at 16:30

1 Answers1

2

Double.valueOf() supports this converting from a String:

System.out.println(Double.valueOf("0x0.1p0"));
System.out.println(Double.valueOf("0x11.11p0"));
System.out.println(Double.valueOf("0x1p3"));

Output:

0.0625
17.06640625
8

Also worth mentioning is that 1.5 added the support for these as literals:

double d = 0x1p3;
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Yeah, I thought that might be the issue (the `p` being required even when `0`) but I didn't want to assume. Glad I could help. – Brian Roach Jan 18 '14 at 21:17