< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
rev 14560 : Merge


 322      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 323      *
 324      * @param   i   an integer to be converted to a string.
 325      * @return  the string representation of the unsigned integer value
 326      *          represented by the argument in binary (base&nbsp;2).
 327      * @see #parseUnsignedInt(String, int)
 328      * @see #toUnsignedString(int, int)
 329      * @since   1.0.2
 330      */
 331     public static String toBinaryString(int i) {
 332         return toUnsignedString0(i, 1);
 333     }
 334 
 335     /**
 336      * Convert the integer to an unsigned number.
 337      */
 338     private static String toUnsignedString0(int val, int shift) {
 339         // assert shift > 0 && shift <=5 : "Illegal shift value";
 340         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 341         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 342 
 343         if (COMPACT_STRINGS) {
 344             byte[] buf = new byte[chars];
 345             formatUnsignedInt(val, shift, buf, 0, chars);
 346             return new String(buf, LATIN1);
 347         } else {
 348             byte[] buf = new byte[chars * 2];
 349             formatUnsignedIntUTF16(val, shift, buf, 0, chars);
 350             return new String(buf, UTF16);
 351         }
 352     }
 353 
 354     /**
 355      * Format an {@code int} (treated as unsigned) into a character buffer. If
 356      * {@code len} exceeds the formatted ASCII representation of {@code val},
 357      * {@code buf} will be padded with leading zeroes.
 358      *
 359      * @param val the unsigned int to format
 360      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 361      * @param buf the character buffer to write to
 362      * @param offset the offset in the destination buffer to start at


 460      * @param   i  an integer to be converted to an unsigned string.
 461      * @return  an unsigned string representation of the argument.
 462      * @see     #toUnsignedString(int, int)
 463      * @since 1.8
 464      */
 465     public static String toUnsignedString(int i) {
 466         return Long.toString(toUnsignedLong(i));
 467     }
 468 
 469     /**
 470      * Places characters representing the integer i into the
 471      * character array buf. The characters are placed into
 472      * the buffer backwards starting with the least significant
 473      * digit at the specified index (exclusive), and working
 474      * backwards from there.
 475      *
 476      * @implNote This method converts positive inputs into negative
 477      * values, to cover the Integer.MIN_VALUE case. Converting otherwise
 478      * (negative to positive) will expose -Integer.MIN_VALUE that overflows
 479      * integer.





 480      */
 481     static void getChars(int i, int index, byte[] buf) {
 482         int q, r;
 483         int charPos = index;
 484 
 485         boolean negative = i < 0;
 486         if (!negative) {
 487             i = -i;
 488         }
 489 
 490         // Generate two digits per iteration
 491         while (i <= -100) {
 492             q = i / 100;
 493             r = (q * 100) - i;
 494             i = q;
 495             buf[--charPos] = DigitOnes[r];
 496             buf[--charPos] = DigitTens[r];
 497         }
 498 
 499         // We know there are at most two digits left at this point.
 500         q = i / 10;
 501         r = (q * 10) - i;
 502         buf[--charPos] = (byte)('0' + r);
 503 
 504         // Whatever left is the remaining digit.
 505         if (q < 0) {
 506             buf[--charPos] = (byte)('0' - q);
 507         }
 508 
 509         if (negative) {
 510             buf[--charPos] = (byte)'-';
 511         }

 512     }
 513 
 514     static void getCharsUTF16(int i, int index, byte[] buf) {









 515         int q, r;
 516         int charPos = index;
 517 
 518         boolean negative = (i < 0);
 519         if (!negative) {
 520             i = -i;
 521         }
 522 
 523         // Get 2 digits/iteration using ints
 524         while (i <= -100) {
 525             q = i / 100;
 526             r = (q * 100) - i;
 527             i = q;
 528             StringUTF16.putChar(buf, --charPos, DigitOnes[r]);
 529             StringUTF16.putChar(buf, --charPos, DigitTens[r]);
 530         }
 531 
 532         // We know there are at most two digits left at this point.
 533         q = i / 10;
 534         r = (q * 10) - i;
 535         StringUTF16.putChar(buf, --charPos, '0' + r);
 536 
 537         // Whatever left is the remaining digit.
 538         if (q < 0) {
 539             StringUTF16.putChar(buf, --charPos, '0' - q);
 540         }
 541 
 542         if (negative) {
 543             StringUTF16.putChar(buf, --charPos, '-');
 544         }

 545     }
 546 
 547     // Left here for compatibility reasons, see JDK-8143900.
 548     static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
 549                                       99999999, 999999999, Integer.MAX_VALUE };
 550 
 551     /**
 552      * Returns the string representation size for a given int value.
 553      *
 554      * @param x int value
 555      * @return string size
 556      *
 557      * @implNote There are other ways to compute this: e.g. binary search,
 558      * but values are biased heavily towards zero, and therefore linear search
 559      * wins. The iteration results are also routinely inlined in the generated
 560      * code after loop unrolling.
 561      */
 562     static int stringSize(int x) {
 563         int d = 1;
 564         if (x >= 0) {




 322      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 323      *
 324      * @param   i   an integer to be converted to a string.
 325      * @return  the string representation of the unsigned integer value
 326      *          represented by the argument in binary (base&nbsp;2).
 327      * @see #parseUnsignedInt(String, int)
 328      * @see #toUnsignedString(int, int)
 329      * @since   1.0.2
 330      */
 331     public static String toBinaryString(int i) {
 332         return toUnsignedString0(i, 1);
 333     }
 334 
 335     /**
 336      * Convert the integer to an unsigned number.
 337      */
 338     private static String toUnsignedString0(int val, int shift) {
 339         // assert shift > 0 && shift <=5 : "Illegal shift value";
 340         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 341         int chars = Math.max(((mag + (shift - 1)) / shift), 1);

 342         if (COMPACT_STRINGS) {
 343             byte[] buf = new byte[chars];
 344             formatUnsignedInt(val, shift, buf, 0, chars);
 345             return new String(buf, LATIN1);
 346         } else {
 347             byte[] buf = new byte[chars * 2];
 348             formatUnsignedIntUTF16(val, shift, buf, 0, chars);
 349             return new String(buf, UTF16);
 350         }
 351     }
 352 
 353     /**
 354      * Format an {@code int} (treated as unsigned) into a character buffer. If
 355      * {@code len} exceeds the formatted ASCII representation of {@code val},
 356      * {@code buf} will be padded with leading zeroes.
 357      *
 358      * @param val the unsigned int to format
 359      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 360      * @param buf the character buffer to write to
 361      * @param offset the offset in the destination buffer to start at


 459      * @param   i  an integer to be converted to an unsigned string.
 460      * @return  an unsigned string representation of the argument.
 461      * @see     #toUnsignedString(int, int)
 462      * @since 1.8
 463      */
 464     public static String toUnsignedString(int i) {
 465         return Long.toString(toUnsignedLong(i));
 466     }
 467 
 468     /**
 469      * Places characters representing the integer i into the
 470      * character array buf. The characters are placed into
 471      * the buffer backwards starting with the least significant
 472      * digit at the specified index (exclusive), and working
 473      * backwards from there.
 474      *
 475      * @implNote This method converts positive inputs into negative
 476      * values, to cover the Integer.MIN_VALUE case. Converting otherwise
 477      * (negative to positive) will expose -Integer.MIN_VALUE that overflows
 478      * integer.
 479      *
 480      * @param i     value to convert
 481      * @param index next index, after the least significant digit
 482      * @param buf   target buffer, Latin1-encoded
 483      * @return index of the most significant digit or minus sign, if present
 484      */
 485     static int getChars(int i, int index, byte[] buf) {
 486         int q, r;
 487         int charPos = index;
 488 
 489         boolean negative = i < 0;
 490         if (!negative) {
 491             i = -i;
 492         }
 493 
 494         // Generate two digits per iteration
 495         while (i <= -100) {
 496             q = i / 100;
 497             r = (q * 100) - i;
 498             i = q;
 499             buf[--charPos] = DigitOnes[r];
 500             buf[--charPos] = DigitTens[r];
 501         }
 502 
 503         // We know there are at most two digits left at this point.
 504         q = i / 10;
 505         r = (q * 10) - i;
 506         buf[--charPos] = (byte)('0' + r);
 507 
 508         // Whatever left is the remaining digit.
 509         if (q < 0) {
 510             buf[--charPos] = (byte)('0' - q);
 511         }
 512 
 513         if (negative) {
 514             buf[--charPos] = (byte)'-';
 515         }
 516         return charPos;
 517     }
 518 
 519     /**
 520      * This is a variant of {@link #getChars(int, int, byte[])}, but for
 521      * UTF-16 coder.
 522      *
 523      * @param i     value to convert
 524      * @param index next index, after the least significant digit
 525      * @param buf   target buffer, UTF16-coded.
 526      * @return index of the most significant digit or minus sign, if present
 527      */
 528     static int getCharsUTF16(int i, int index, byte[] buf) {
 529         int q, r;
 530         int charPos = index;
 531 
 532         boolean negative = (i < 0);
 533         if (!negative) {
 534             i = -i;
 535         }
 536 
 537         // Get 2 digits/iteration using ints
 538         while (i <= -100) {
 539             q = i / 100;
 540             r = (q * 100) - i;
 541             i = q;
 542             StringUTF16.putChar(buf, --charPos, DigitOnes[r]);
 543             StringUTF16.putChar(buf, --charPos, DigitTens[r]);
 544         }
 545 
 546         // We know there are at most two digits left at this point.
 547         q = i / 10;
 548         r = (q * 10) - i;
 549         StringUTF16.putChar(buf, --charPos, '0' + r);
 550 
 551         // Whatever left is the remaining digit.
 552         if (q < 0) {
 553             StringUTF16.putChar(buf, --charPos, '0' - q);
 554         }
 555 
 556         if (negative) {
 557             StringUTF16.putChar(buf, --charPos, '-');
 558         }
 559         return charPos;
 560     }
 561 
 562     // Left here for compatibility reasons, see JDK-8143900.
 563     static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
 564                                       99999999, 999999999, Integer.MAX_VALUE };
 565 
 566     /**
 567      * Returns the string representation size for a given int value.
 568      *
 569      * @param x int value
 570      * @return string size
 571      *
 572      * @implNote There are other ways to compute this: e.g. binary search,
 573      * but values are biased heavily towards zero, and therefore linear search
 574      * wins. The iteration results are also routinely inlined in the generated
 575      * code after loop unrolling.
 576      */
 577     static int stringSize(int x) {
 578         int d = 1;
 579         if (x >= 0) {


< prev index next >