예제 #1
0
  /*
   * Transform the hexadecimal long output into the equivalent
   * hexadecimal double value.
   */
  static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for (int i = 0; i < (16 - transString.length()); i++, zeros += "0") ;
    transString = zeros + transString;

    // assert transString.length == 16;

    char topChar;
    // Extract sign
    if ((topChar = transString.charAt(0)) >= '8') { // 8, 9, a, A, b, B, ...
      result.append("-");
      // clear sign bit
      transString =
          Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16))
              + transString.substring(1, 16);
    }

    // check for NaN and infinity
    String signifString = transString.substring(3, 16);

    if (transString.substring(0, 3).equals("7ff")) {
      if (signifString.equals("0000000000000")) {
        result.append("Infinity");
      } else result.append("NaN");
    } else { // finite value
      // Extract exponent
      int exponent = Integer.parseInt(transString.substring(0, 3), 16) - DoubleConsts.EXP_BIAS;
      result.append("0x");

      if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
        if (signifString.equals("0000000000000")) {
          result.append("0.0p0");
        } else {
          result.append(
              "0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") + "p-1022");
        }
      } else { // normal value
        result.append(
            "1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") + "p" + exponent);
      }
    }
    return result.toString();
  }