MCPcopy Create free account
hub / github.com/davidgiven/luje / toHexString

Method toHexString

lib/java/lang/Double.java:393–475  ·  view source on GitHub ↗

Converts the specified double into its hexadecimal string representation. @param d the double to convert. @return the hexadecimal string representation of d. @since 1.5

(double d)

Source from the content-addressed store, hash-verified

391 * @since 1.5
392 */
393 public static String toHexString(double d) {
394 /*
395 * Reference: http://en.wikipedia.org/wiki/IEEE_754
396 */
397 if (d != d) {
398 return "NaN"; //$NON-NLS-1$
399 }
400 if (d == POSITIVE_INFINITY) {
401 return "Infinity"; //$NON-NLS-1$
402 }
403 if (d == NEGATIVE_INFINITY) {
404 return "-Infinity"; //$NON-NLS-1$
405 }
406
407 long bitValue = doubleToLongBits(d);
408
409 boolean negative = (bitValue & 0x8000000000000000L) != 0;
410 // mask exponent bits and shift down
411 long exponent = (bitValue & 0x7FF0000000000000L) >>> 52;
412 // mask significand bits and shift up
413 long significand = bitValue & 0x000FFFFFFFFFFFFFL;
414
415 if (exponent == 0 && significand == 0) {
416 return (negative ? "-0x0.0p0" : "0x0.0p0"); //$NON-NLS-1$ //$NON-NLS-2$
417 }
418
419 StringBuilder hexString = new StringBuilder(10);
420 if (negative) {
421 hexString.append("-0x"); //$NON-NLS-1$
422 } else {
423 hexString.append("0x"); //$NON-NLS-1$
424 }
425
426 if (exponent == 0) { // denormal (subnormal) value
427 hexString.append("0."); //$NON-NLS-1$
428 // significand is 52-bits, so there can be 13 hex digits
429 int fractionDigits = 13;
430 // remove trailing hex zeros, so Integer.toHexString() won't print
431 // them
432 while ((significand != 0) && ((significand & 0xF) == 0)) {
433 significand >>>= 4;
434 fractionDigits--;
435 }
436 // this assumes Integer.toHexString() returns lowercase characters
437 String hexSignificand = Long.toHexString(significand);
438
439 // if there are digits left, then insert some '0' chars first
440 if (significand != 0 && fractionDigits > hexSignificand.length()) {
441 int digitDiff = fractionDigits - hexSignificand.length();
442 while (digitDiff-- != 0) {
443 hexString.append('0');
444 }
445 }
446 hexString.append(hexSignificand);
447 hexString.append("p-1022"); //$NON-NLS-1$
448 } else { // normal value
449 hexString.append("1."); //$NON-NLS-1$
450 // significand is 52-bits, so there can be 13 hex digits

Callers

nothing calls this directly

Calls 6

doubleToLongBitsMethod · 0.95
appendMethod · 0.95
toHexStringMethod · 0.95
lengthMethod · 0.95
toStringMethod · 0.95
toStringMethod · 0.95

Tested by

no test coverage detected