title: Double to Hex String and Back
body: <p>It's fairly simple to convert a double to a hexadecimal string in Java. But how do I do the reverse? My code is below and I've noted where a <code>NumberFormatException</code> is thrown (about 2/3rds down).</p>
  
  <pre><code>public class HexToDoubleTest {
      public static void main( String args[] ) {
  
          // This is the starting double value
          double doubleInput = -9.156013e-002;
  
          // Convert the starting value to the equivalent value in a long
          long doubleAsLong = Double.doubleToRawLongBits( doubleInput );
  
          // Convert the long to a String
          String doubleAsString = Long.toHexString( doubleAsLong );
  
          // Print the String
          System.out.println( doubleAsString );
  
          // Attempt to parse the string back as a long
          // !!! This fails with a NumberFormatException !!!
          long doubleAsLongReverse = Long.parseLong( doubleAsString, 16 );
  
          // Convert the long back into the original double
          double doubleOutput = Double.longBitsToDouble( doubleAsLongReverse );
  
          // Confirm that the values match
          assert( doubleInput == doubleOutput );
  
      }
  }
  </code></pre>
  
  <p>Using <code>Double.valueOf</code> fails in the same manner.</p>
  
  <p>Edit: I've done a few searches on the web already and found some very inelegant solutions. For example: Using a <code>BigInteger</code> seems like overkill. There's got to be a better way!</p>