7 Replies Latest reply: Jan 18, 2008 4:55 PM by 807603 RSS

    How to take a negative hex double number into a Double variable?

    807603
      Hi all,

      I've got the problem of taking a string representing a negative double hex number like BFF0000000000000 (which is -1 in decimal) to a double type variable. Converting positive double hex numbers works fine, but as soon as i try to convert any negative number my program throws an exception.
      conversion tool:
      http://babbage.cs.qc.edu/IEEE-754/Decimal.html

      I had to do this conversion method for float too, and noticed that in order to convert negative numbers too i had to make the parse with a higer type (long):

      public static float hexToFloat(String hexValue)
      {
      float floatVal= 0.0f;

      // Parse string interpreting data as a hex number into an int.
      int decimalValue = (int)Long.parseLong(hexValue,16);

      // Reinterpret the int variable's bits as a float type.
      floatVal=Float.intBitsToFloat(decimalValue);

      return floatVal;
      }

      But in the case of Double there is no higher type, any suggestions?.

      Thanks in advance!