321 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
322 *
323 * @param i an integer to be converted to a string.
324 * @return the string representation of the unsigned integer value
325 * represented by the argument in binary (base 2).
326 * @see #parseUnsignedInt(String, int)
327 * @see #toUnsignedString(int, int)
328 * @since 1.0.2
329 */
330 public static String toBinaryString(int i) {
331 return toUnsignedString0(i, 1);
332 }
333
334 /**
335 * Convert the integer to an unsigned number.
336 */
337 private static String toUnsignedString0(int val, int shift) {
338 // assert shift > 0 && shift <=5 : "Illegal shift value";
339 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
340 int chars = Math.max(((mag + (shift - 1)) / shift), 1);
341
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 static void getChars(int i, int index, byte[] buf) {
481 int q, r;
482 int charPos = index;
483
484 boolean negative = i < 0;
485 if (!negative) {
486 i = -i;
487 }
488
489 // Generate two digits per iteration
490 while (i <= -100) {
491 q = i / 100;
492 r = (q * 100) - i;
493 i = q;
494 buf[--charPos] = DigitOnes[r];
495 buf[--charPos] = DigitTens[r];
496 }
497
498 // We know there are at most two digits left at this point.
499 q = i / 10;
500 r = (q * 10) - i;
501 buf[--charPos] = (byte)('0' + r);
502
503 // Whatever left is the remaining digit.
504 if (q < 0) {
505 buf[--charPos] = (byte)('0' - q);
506 }
507
508 if (negative) {
509 buf[--charPos] = (byte)'-';
510 }
511 }
512
513 static void getCharsUTF16(int i, int index, byte[] buf) {
514 int q, r;
515 int charPos = index;
516
517 boolean negative = (i < 0);
518 if (!negative) {
519 i = -i;
520 }
521
522 // Get 2 digits/iteration using ints
523 while (i <= -100) {
524 q = i / 100;
525 r = (q * 100) - i;
526 i = q;
527 StringUTF16.putChar(buf, --charPos, DigitOnes[r]);
528 StringUTF16.putChar(buf, --charPos, DigitTens[r]);
529 }
530
531 // We know there are at most two digits left at this point.
532 q = i / 10;
533 r = (q * 10) - i;
534 StringUTF16.putChar(buf, --charPos, '0' + r);
535
536 // Whatever left is the remaining digit.
537 if (q < 0) {
538 StringUTF16.putChar(buf, --charPos, '0' - q);
539 }
540
541 if (negative) {
542 StringUTF16.putChar(buf, --charPos, '-');
543 }
544 }
545
546 // Left here for compatibility reasons, see JDK-8143900.
547 static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
548 99999999, 999999999, Integer.MAX_VALUE };
549
550 /**
551 * Returns the string representation size for a given int value.
552 *
553 * @param x int value
554 * @return string size
555 *
556 * @implNote There are other ways to compute this: e.g. binary search,
557 * but values are biased heavily towards zero, and therefore linear search
558 * wins. The iteration results are also routinely inlined in the generated
559 * code after loop unrolling.
560 */
561 static int stringSize(int x) {
562 int d = 1;
563 if (x >= 0) {
|
321 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
322 *
323 * @param i an integer to be converted to a string.
324 * @return the string representation of the unsigned integer value
325 * represented by the argument in binary (base 2).
326 * @see #parseUnsignedInt(String, int)
327 * @see #toUnsignedString(int, int)
328 * @since 1.0.2
329 */
330 public static String toBinaryString(int i) {
331 return toUnsignedString0(i, 1);
332 }
333
334 /**
335 * Convert the integer to an unsigned number.
336 */
337 private static String toUnsignedString0(int val, int shift) {
338 // assert shift > 0 && shift <=5 : "Illegal shift value";
339 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
340 int chars = Math.max(((mag + (shift - 1)) / shift), 1);
341 if (COMPACT_STRINGS) {
342 byte[] buf = new byte[chars];
343 formatUnsignedInt(val, shift, buf, 0, chars);
344 return new String(buf, LATIN1);
345 } else {
346 byte[] buf = new byte[chars * 2];
347 formatUnsignedIntUTF16(val, shift, buf, 0, chars);
348 return new String(buf, UTF16);
349 }
350 }
351
352 /**
353 * Format an {@code int} (treated as unsigned) into a character buffer. If
354 * {@code len} exceeds the formatted ASCII representation of {@code val},
355 * {@code buf} will be padded with leading zeroes.
356 *
357 * @param val the unsigned int to format
358 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
359 * @param buf the character buffer to write to
360 * @param offset the offset in the destination buffer to start at
458 * @param i an integer to be converted to an unsigned string.
459 * @return an unsigned string representation of the argument.
460 * @see #toUnsignedString(int, int)
461 * @since 1.8
462 */
463 public static String toUnsignedString(int i) {
464 return Long.toString(toUnsignedLong(i));
465 }
466
467 /**
468 * Places characters representing the integer i into the
469 * character array buf. The characters are placed into
470 * the buffer backwards starting with the least significant
471 * digit at the specified index (exclusive), and working
472 * backwards from there.
473 *
474 * @implNote This method converts positive inputs into negative
475 * values, to cover the Integer.MIN_VALUE case. Converting otherwise
476 * (negative to positive) will expose -Integer.MIN_VALUE that overflows
477 * integer.
478 *
479 * @param i value to convert
480 * @param index next index, after the least significant digit
481 * @param buf target buffer, Latin1-encoded
482 * @return index of the most significant digit or minus sign, if present
483 */
484 static int getChars(int i, int index, byte[] buf) {
485 int q, r;
486 int charPos = index;
487
488 boolean negative = i < 0;
489 if (!negative) {
490 i = -i;
491 }
492
493 // Generate two digits per iteration
494 while (i <= -100) {
495 q = i / 100;
496 r = (q * 100) - i;
497 i = q;
498 buf[--charPos] = DigitOnes[r];
499 buf[--charPos] = DigitTens[r];
500 }
501
502 // We know there are at most two digits left at this point.
503 q = i / 10;
504 r = (q * 10) - i;
505 buf[--charPos] = (byte)('0' + r);
506
507 // Whatever left is the remaining digit.
508 if (q < 0) {
509 buf[--charPos] = (byte)('0' - q);
510 }
511
512 if (negative) {
513 buf[--charPos] = (byte)'-';
514 }
515 return charPos;
516 }
517
518 /**
519 * This is a variant of {@link #getChars(int, int, byte[])}, but for
520 * UTF-16 coder.
521 *
522 * @param i value to convert
523 * @param index next index, after the least significant digit
524 * @param buf target buffer, UTF16-coded.
525 * @return index of the most significant digit or minus sign, if present
526 */
527 static int getCharsUTF16(int i, int index, byte[] buf) {
528 int q, r;
529 int charPos = index;
530
531 boolean negative = (i < 0);
532 if (!negative) {
533 i = -i;
534 }
535
536 // Get 2 digits/iteration using ints
537 while (i <= -100) {
538 q = i / 100;
539 r = (q * 100) - i;
540 i = q;
541 StringUTF16.putChar(buf, --charPos, DigitOnes[r]);
542 StringUTF16.putChar(buf, --charPos, DigitTens[r]);
543 }
544
545 // We know there are at most two digits left at this point.
546 q = i / 10;
547 r = (q * 10) - i;
548 StringUTF16.putChar(buf, --charPos, '0' + r);
549
550 // Whatever left is the remaining digit.
551 if (q < 0) {
552 StringUTF16.putChar(buf, --charPos, '0' - q);
553 }
554
555 if (negative) {
556 StringUTF16.putChar(buf, --charPos, '-');
557 }
558 return charPos;
559 }
560
561 // Left here for compatibility reasons, see JDK-8143900.
562 static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
563 99999999, 999999999, Integer.MAX_VALUE };
564
565 /**
566 * Returns the string representation size for a given int value.
567 *
568 * @param x int value
569 * @return string size
570 *
571 * @implNote There are other ways to compute this: e.g. binary search,
572 * but values are biased heavily towards zero, and therefore linear search
573 * wins. The iteration results are also routinely inlined in the generated
574 * code after loop unrolling.
575 */
576 static int stringSize(int x) {
577 int d = 1;
578 if (x >= 0) {
|