76 */
77 final static char[] digits = {
78 '0' , '1' , '2' , '3' , '4' , '5' ,
79 '6' , '7' , '8' , '9' , 'a' , 'b' ,
80 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
81 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
82 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
83 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
84 };
85
86 /**
87 * Returns a string representation of the first argument in the
88 * radix specified by the second argument.
89 *
90 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
91 * or larger than {@code Character.MAX_RADIX}, then the radix
92 * {@code 10} is used instead.
93 *
94 * <p>If the first argument is negative, the first element of the
95 * result is the ASCII minus character {@code '-'}
96 * (<code>'\u002D'</code>). If the first argument is not
97 * negative, no sign character appears in the result.
98 *
99 * <p>The remaining characters of the result represent the magnitude
100 * of the first argument. If the magnitude is zero, it is
101 * represented by a single zero character {@code '0'}
102 * (<code>'\u0030'</code>); otherwise, the first character of
103 * the representation of the magnitude will not be the zero
104 * character. The following ASCII characters are used as digits:
105 *
106 * <blockquote>
107 * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
108 * </blockquote>
109 *
110 * These are <code>'\u0030'</code> through
111 * <code>'\u0039'</code> and <code>'\u0061'</code> through
112 * <code>'\u007A'</code>. If {@code radix} is
113 * <var>N</var>, then the first <var>N</var> of these characters
114 * are used as radix-<var>N</var> digits in the order shown. Thus,
115 * the digits for hexadecimal (radix 16) are
116 * {@code 0123456789abcdef}. If uppercase letters are
117 * desired, the {@link java.lang.String#toUpperCase()} method may
118 * be called on the result:
119 *
120 * <blockquote>
121 * {@code Integer.toString(n, 16).toUpperCase()}
122 * </blockquote>
123 *
124 * @param i an integer to be converted to a string.
125 * @param radix the radix to use in the string representation.
126 * @return a string representation of the argument in the specified radix.
127 * @see java.lang.Character#MAX_RADIX
128 * @see java.lang.Character#MIN_RADIX
129 */
130 public static String toString(int i, int radix) {
131 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
132 radix = 10;
153 if (negative) {
154 buf[--charPos] = '-';
155 }
156
157 return new String(buf, charPos, (33 - charPos));
158 }
159
160 /**
161 * Returns a string representation of the first argument as an
162 * unsigned integer value in the radix specified by the second
163 * argument.
164 *
165 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
166 * or larger than {@code Character.MAX_RADIX}, then the radix
167 * {@code 10} is used instead.
168 *
169 * <p>Note that since the first argument is treated as an unsigned
170 * value, no leading sign character is printed.
171 *
172 * <p>If the magnitude is zero, it is represented by a single zero
173 * character {@code '0'} (<code>'\u0030'</code>); otherwise,
174 * the first character of the representation of the magnitude will
175 * not be the zero character.
176 *
177 * <p>The behavior of radixes and the characters used as digits
178 * are the same as {@link #toString(int, int) toString}.
179 *
180 * @param i an integer to be converted to an unsigned string.
181 * @param radix the radix to use in the string representation.
182 * @return an unsigned string representation of the argument in the specified radix.
183 * @see #toString(int, int)
184 * @since 1.8
185 */
186 public static String toUnsignedString(int i, int radix) {
187 return Long.toString(toUnsignedLong(i), radix);
188 }
189
190 /**
191 * Returns a string representation of the integer argument as an
192 * unsigned integer in base 16.
193 *
194 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
195 * if the argument is negative; otherwise, it is equal to the
196 * argument. This value is converted to a string of ASCII digits
197 * in hexadecimal (base 16) with no extra leading
198 * {@code 0}s.
199 *
200 * <p>The value of the argument can be recovered from the returned
201 * string {@code s} by calling {@link
202 * Integer#parseUnsignedInt(String, int)
203 * Integer.parseUnsignedInt(s, 16)}.
204 *
205 * <p>If the unsigned magnitude is zero, it is represented by a
206 * single zero character {@code '0'} (<code>'\u0030'</code>);
207 * otherwise, the first character of the representation of the
208 * unsigned magnitude will not be the zero character. The
209 * following characters are used as hexadecimal digits:
210 *
211 * <blockquote>
212 * {@code 0123456789abcdef}
213 * </blockquote>
214 *
215 * These are the characters <code>'\u0030'</code> through
216 * <code>'\u0039'</code> and <code>'\u0061'</code> through
217 * <code>'\u0066'</code>. If uppercase letters are
218 * desired, the {@link java.lang.String#toUpperCase()} method may
219 * be called on the result:
220 *
221 * <blockquote>
222 * {@code Integer.toHexString(n).toUpperCase()}
223 * </blockquote>
224 *
225 * @param i an integer to be converted to a string.
226 * @return the string representation of the unsigned integer value
227 * represented by the argument in hexadecimal (base 16).
228 * @see #parseUnsignedInt(String, int)
229 * @see #toUnsignedString(int, int)
230 * @since JDK1.0.2
231 */
232 public static String toHexString(int i) {
233 return toUnsignedString0(i, 4);
234 }
235
236 /**
237 * Returns a string representation of the integer argument as an
238 * unsigned integer in base 8.
239 *
240 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
241 * if the argument is negative; otherwise, it is equal to the
242 * argument. This value is converted to a string of ASCII digits
243 * in octal (base 8) with no extra leading {@code 0}s.
244 *
245 * <p>The value of the argument can be recovered from the returned
246 * string {@code s} by calling {@link
247 * Integer#parseUnsignedInt(String, int)
248 * Integer.parseUnsignedInt(s, 8)}.
249 *
250 * <p>If the unsigned magnitude is zero, it is represented by a
251 * single zero character {@code '0'} (<code>'\u0030'</code>);
252 * otherwise, the first character of the representation of the
253 * unsigned magnitude will not be the zero character. The
254 * following characters are used as octal digits:
255 *
256 * <blockquote>
257 * {@code 01234567}
258 * </blockquote>
259 *
260 * These are the characters <code>'\u0030'</code> through
261 * <code>'\u0037'</code>.
262 *
263 * @param i an integer to be converted to a string.
264 * @return the string representation of the unsigned integer value
265 * represented by the argument in octal (base 8).
266 * @see #parseUnsignedInt(String, int)
267 * @see #toUnsignedString(int, int)
268 * @since JDK1.0.2
269 */
270 public static String toOctalString(int i) {
271 return toUnsignedString0(i, 3);
272 }
273
274 /**
275 * Returns a string representation of the integer argument as an
276 * unsigned integer in base 2.
277 *
278 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
279 * if the argument is negative; otherwise it is equal to the
280 * argument. This value is converted to a string of ASCII digits
281 * in binary (base 2) with no extra leading {@code 0}s.
282 *
283 * <p>The value of the argument can be recovered from the returned
284 * string {@code s} by calling {@link
285 * Integer#parseUnsignedInt(String, int)
286 * Integer.parseUnsignedInt(s, 2)}.
287 *
288 * <p>If the unsigned magnitude is zero, it is represented by a
289 * single zero character {@code '0'} (<code>'\u0030'</code>);
290 * otherwise, the first character of the representation of the
291 * unsigned magnitude will not be the zero character. The
292 * characters {@code '0'} (<code>'\u0030'</code>) and {@code
293 * '1'} (<code>'\u0031'</code>) are used as binary digits.
294 *
295 * @param i an integer to be converted to a string.
296 * @return the string representation of the unsigned integer value
297 * represented by the argument in binary (base 2).
298 * @see #parseUnsignedInt(String, int)
299 * @see #toUnsignedString(int, int)
300 * @since JDK1.0.2
301 */
302 public static String toBinaryString(int i) {
303 return toUnsignedString0(i, 1);
304 }
305
306 /**
307 * Convert the integer to an unsigned number.
308 */
309 private static String toUnsignedString0(int i, int shift) {
310 char[] buf = new char[32];
311 int charPos = 32;
312 int radix = 1 << shift;
313 int mask = radix - 1;
444 buf [--charPos] = sign;
445 }
446 }
447
448 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
449 99999999, 999999999, Integer.MAX_VALUE };
450
451 // Requires positive x
452 static int stringSize(int x) {
453 for (int i=0; ; i++)
454 if (x <= sizeTable[i])
455 return i+1;
456 }
457
458 /**
459 * Parses the string argument as a signed integer in the radix
460 * specified by the second argument. The characters in the string
461 * must all be digits of the specified radix (as determined by
462 * whether {@link java.lang.Character#digit(char, int)} returns a
463 * nonnegative value), except that the first character may be an
464 * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
465 * indicate a negative value or an ASCII plus sign {@code '+'}
466 * (<code>'\u002B'</code>) to indicate a positive value. The
467 * resulting integer value is returned.
468 *
469 * <p>An exception of type {@code NumberFormatException} is
470 * thrown if any of the following situations occurs:
471 * <ul>
472 * <li>The first argument is {@code null} or is a string of
473 * length zero.
474 *
475 * <li>The radix is either smaller than
476 * {@link java.lang.Character#MIN_RADIX} or
477 * larger than {@link java.lang.Character#MAX_RADIX}.
478 *
479 * <li>Any character of the string is not a digit of the specified
480 * radix, except that the first character may be a minus sign
481 * {@code '-'} (<code>'\u002D'</code>) or plus sign
482 * {@code '+'} (<code>'\u002B'</code>) provided that the
483 * string is longer than length 1.
484 *
485 * <li>The value represented by the string is not a value of type
486 * {@code int}.
487 * </ul>
488 *
489 * <p>Examples:
490 * <blockquote><pre>
491 * parseInt("0", 10) returns 0
492 * parseInt("473", 10) returns 473
493 * parseInt("+42", 10) returns 42
494 * parseInt("-0", 10) returns 0
495 * parseInt("-FF", 16) returns -255
496 * parseInt("1100110", 2) returns 102
497 * parseInt("2147483647", 10) returns 2147483647
498 * parseInt("-2147483648", 10) returns -2147483648
499 * parseInt("2147483648", 10) throws a NumberFormatException
500 * parseInt("99", 8) throws a NumberFormatException
501 * parseInt("Kona", 10) throws a NumberFormatException
502 * parseInt("Kona", 27) returns 411787
562 }
563 if (result < multmin) {
564 throw NumberFormatException.forInputString(s);
565 }
566 result *= radix;
567 if (result < limit + digit) {
568 throw NumberFormatException.forInputString(s);
569 }
570 result -= digit;
571 }
572 } else {
573 throw NumberFormatException.forInputString(s);
574 }
575 return negative ? result : -result;
576 }
577
578 /**
579 * Parses the string argument as a signed decimal integer. The
580 * characters in the string must all be decimal digits, except
581 * that the first character may be an ASCII minus sign {@code '-'}
582 * (<code>'\u002D'</code>) to indicate a negative value or an
583 * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
584 * indicate a positive value. The resulting integer value is
585 * returned, exactly as if the argument and the radix 10 were
586 * given as arguments to the {@link #parseInt(java.lang.String,
587 * int)} method.
588 *
589 * @param s a {@code String} containing the {@code int}
590 * representation to be parsed
591 * @return the integer value represented by the argument in decimal.
592 * @exception NumberFormatException if the string does not contain a
593 * parsable integer.
594 */
595 public static int parseInt(String s) throws NumberFormatException {
596 return parseInt(s,10);
597 }
598
599 /**
600 * Parses the string argument as an unsigned integer in the radix
601 * specified by the second argument. An unsigned integer maps the
602 * values usually associated with negative numbers to positive
603 * numbers larger than {@code MAX_VALUE}.
604 *
605 * The characters in the string must all be digits of the
606 * specified radix (as determined by whether {@link
607 * java.lang.Character#digit(char, int)} returns a nonnegative
608 * value), except that the first character may be an ASCII plus
609 * sign {@code '+'} (<code>'\u002B'</code>). The resulting
610 * integer value is returned.
611 *
612 * <p>An exception of type {@code NumberFormatException} is
613 * thrown if any of the following situations occurs:
614 * <ul>
615 * <li>The first argument is {@code null} or is a string of
616 * length zero.
617 *
618 * <li>The radix is either smaller than
619 * {@link java.lang.Character#MIN_RADIX} or
620 * larger than {@link java.lang.Character#MAX_RADIX}.
621 *
622 * <li>Any character of the string is not a digit of the specified
623 * radix, except that the first character may be a plus sign
624 * {@code '+'} (<code>'\u002B'</code>) provided that the
625 * string is longer than length 1.
626 *
627 * <li>The value represented by the string is larger than the
628 * largest unsigned {@code int}, 2<sup>32</sup>-1.
629 *
630 * </ul>
631 *
632 *
633 * @param s the {@code String} containing the unsigned integer
634 * representation to be parsed
635 * @param radix the radix to be used while parsing {@code s}.
636 * @return the integer represented by the string argument in the
637 * specified radix.
638 * @throws NumberFormatException if the {@code String}
639 * does not contain a parsable {@code int}.
640 * @since 1.8
641 */
642 public static int parseUnsignedInt(String s, int radix)
643 throws NumberFormatException {
644 if (s == null) {
659 } else {
660 long ell = Long.parseLong(s, radix);
661 if ((ell & 0xffff_ffff_0000_0000L) == 0) {
662 return (int) ell;
663 } else {
664 throw new
665 NumberFormatException(String.format("String value %s exceeds " +
666 "range of unsigned int.", s));
667 }
668 }
669 }
670 } else {
671 throw NumberFormatException.forInputString(s);
672 }
673 }
674
675 /**
676 * Parses the string argument as an unsigned decimal integer. The
677 * characters in the string must all be decimal digits, except
678 * that the first character may be an an ASCII plus sign {@code
679 * '+'} (<code>'\u002B'</code>). The resulting integer value
680 * is returned, exactly as if the argument and the radix 10 were
681 * given as arguments to the {@link
682 * #parseUnsignedInt(java.lang.String, int)} method.
683 *
684 * @param s a {@code String} containing the unsigned {@code int}
685 * representation to be parsed
686 * @return the unsigned integer value represented by the argument in decimal.
687 * @throws NumberFormatException if the string does not contain a
688 * parsable unsigned integer.
689 * @since 1.8
690 */
691 public static int parseUnsignedInt(String s) throws NumberFormatException {
692 return parseUnsignedInt(s, 10);
693 }
694
695 /**
696 * Returns an {@code Integer} object holding the value
697 * extracted from the specified {@code String} when parsed
698 * with the radix given by the second argument. The first argument
699 * is interpreted as representing a signed integer in the radix
|
76 */
77 final static char[] digits = {
78 '0' , '1' , '2' , '3' , '4' , '5' ,
79 '6' , '7' , '8' , '9' , 'a' , 'b' ,
80 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
81 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
82 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
83 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
84 };
85
86 /**
87 * Returns a string representation of the first argument in the
88 * radix specified by the second argument.
89 *
90 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
91 * or larger than {@code Character.MAX_RADIX}, then the radix
92 * {@code 10} is used instead.
93 *
94 * <p>If the first argument is negative, the first element of the
95 * result is the ASCII minus character {@code '-'}
96 * ({@code '\u005Cu002D'}). If the first argument is not
97 * negative, no sign character appears in the result.
98 *
99 * <p>The remaining characters of the result represent the magnitude
100 * of the first argument. If the magnitude is zero, it is
101 * represented by a single zero character {@code '0'}
102 * ({@code '\u005Cu0030'}); otherwise, the first character of
103 * the representation of the magnitude will not be the zero
104 * character. The following ASCII characters are used as digits:
105 *
106 * <blockquote>
107 * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
108 * </blockquote>
109 *
110 * These are {@code '\u005Cu0030'} through
111 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
112 * {@code '\u005Cu007A'}. If {@code radix} is
113 * <var>N</var>, then the first <var>N</var> of these characters
114 * are used as radix-<var>N</var> digits in the order shown. Thus,
115 * the digits for hexadecimal (radix 16) are
116 * {@code 0123456789abcdef}. If uppercase letters are
117 * desired, the {@link java.lang.String#toUpperCase()} method may
118 * be called on the result:
119 *
120 * <blockquote>
121 * {@code Integer.toString(n, 16).toUpperCase()}
122 * </blockquote>
123 *
124 * @param i an integer to be converted to a string.
125 * @param radix the radix to use in the string representation.
126 * @return a string representation of the argument in the specified radix.
127 * @see java.lang.Character#MAX_RADIX
128 * @see java.lang.Character#MIN_RADIX
129 */
130 public static String toString(int i, int radix) {
131 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
132 radix = 10;
153 if (negative) {
154 buf[--charPos] = '-';
155 }
156
157 return new String(buf, charPos, (33 - charPos));
158 }
159
160 /**
161 * Returns a string representation of the first argument as an
162 * unsigned integer value in the radix specified by the second
163 * argument.
164 *
165 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
166 * or larger than {@code Character.MAX_RADIX}, then the radix
167 * {@code 10} is used instead.
168 *
169 * <p>Note that since the first argument is treated as an unsigned
170 * value, no leading sign character is printed.
171 *
172 * <p>If the magnitude is zero, it is represented by a single zero
173 * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
174 * the first character of the representation of the magnitude will
175 * not be the zero character.
176 *
177 * <p>The behavior of radixes and the characters used as digits
178 * are the same as {@link #toString(int, int) toString}.
179 *
180 * @param i an integer to be converted to an unsigned string.
181 * @param radix the radix to use in the string representation.
182 * @return an unsigned string representation of the argument in the specified radix.
183 * @see #toString(int, int)
184 * @since 1.8
185 */
186 public static String toUnsignedString(int i, int radix) {
187 return Long.toString(toUnsignedLong(i), radix);
188 }
189
190 /**
191 * Returns a string representation of the integer argument as an
192 * unsigned integer in base 16.
193 *
194 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
195 * if the argument is negative; otherwise, it is equal to the
196 * argument. This value is converted to a string of ASCII digits
197 * in hexadecimal (base 16) with no extra leading
198 * {@code 0}s.
199 *
200 * <p>The value of the argument can be recovered from the returned
201 * string {@code s} by calling {@link
202 * Integer#parseUnsignedInt(String, int)
203 * Integer.parseUnsignedInt(s, 16)}.
204 *
205 * <p>If the unsigned magnitude is zero, it is represented by a
206 * single zero character {@code '0'} ({@code '\u005Cu0030'});
207 * otherwise, the first character of the representation of the
208 * unsigned magnitude will not be the zero character. The
209 * following characters are used as hexadecimal digits:
210 *
211 * <blockquote>
212 * {@code 0123456789abcdef}
213 * </blockquote>
214 *
215 * These are the characters {@code '\u005Cu0030'} through
216 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
217 * {@code '\u005Cu0066'}. If uppercase letters are
218 * desired, the {@link java.lang.String#toUpperCase()} method may
219 * be called on the result:
220 *
221 * <blockquote>
222 * {@code Integer.toHexString(n).toUpperCase()}
223 * </blockquote>
224 *
225 * @param i an integer to be converted to a string.
226 * @return the string representation of the unsigned integer value
227 * represented by the argument in hexadecimal (base 16).
228 * @see #parseUnsignedInt(String, int)
229 * @see #toUnsignedString(int, int)
230 * @since JDK1.0.2
231 */
232 public static String toHexString(int i) {
233 return toUnsignedString0(i, 4);
234 }
235
236 /**
237 * Returns a string representation of the integer argument as an
238 * unsigned integer in base 8.
239 *
240 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
241 * if the argument is negative; otherwise, it is equal to the
242 * argument. This value is converted to a string of ASCII digits
243 * in octal (base 8) with no extra leading {@code 0}s.
244 *
245 * <p>The value of the argument can be recovered from the returned
246 * string {@code s} by calling {@link
247 * Integer#parseUnsignedInt(String, int)
248 * Integer.parseUnsignedInt(s, 8)}.
249 *
250 * <p>If the unsigned magnitude is zero, it is represented by a
251 * single zero character {@code '0'} ({@code '\u005Cu0030'});
252 * otherwise, the first character of the representation of the
253 * unsigned magnitude will not be the zero character. The
254 * following characters are used as octal digits:
255 *
256 * <blockquote>
257 * {@code 01234567}
258 * </blockquote>
259 *
260 * These are the characters {@code '\u005Cu0030'} through
261 * {@code '\u005Cu0037'}.
262 *
263 * @param i an integer to be converted to a string.
264 * @return the string representation of the unsigned integer value
265 * represented by the argument in octal (base 8).
266 * @see #parseUnsignedInt(String, int)
267 * @see #toUnsignedString(int, int)
268 * @since JDK1.0.2
269 */
270 public static String toOctalString(int i) {
271 return toUnsignedString0(i, 3);
272 }
273
274 /**
275 * Returns a string representation of the integer argument as an
276 * unsigned integer in base 2.
277 *
278 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
279 * if the argument is negative; otherwise it is equal to the
280 * argument. This value is converted to a string of ASCII digits
281 * in binary (base 2) with no extra leading {@code 0}s.
282 *
283 * <p>The value of the argument can be recovered from the returned
284 * string {@code s} by calling {@link
285 * Integer#parseUnsignedInt(String, int)
286 * Integer.parseUnsignedInt(s, 2)}.
287 *
288 * <p>If the unsigned magnitude is zero, it is represented by a
289 * single zero character {@code '0'} ({@code '\u005Cu0030'});
290 * otherwise, the first character of the representation of the
291 * unsigned magnitude will not be the zero character. The
292 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
293 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
294 *
295 * @param i an integer to be converted to a string.
296 * @return the string representation of the unsigned integer value
297 * represented by the argument in binary (base 2).
298 * @see #parseUnsignedInt(String, int)
299 * @see #toUnsignedString(int, int)
300 * @since JDK1.0.2
301 */
302 public static String toBinaryString(int i) {
303 return toUnsignedString0(i, 1);
304 }
305
306 /**
307 * Convert the integer to an unsigned number.
308 */
309 private static String toUnsignedString0(int i, int shift) {
310 char[] buf = new char[32];
311 int charPos = 32;
312 int radix = 1 << shift;
313 int mask = radix - 1;
444 buf [--charPos] = sign;
445 }
446 }
447
448 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
449 99999999, 999999999, Integer.MAX_VALUE };
450
451 // Requires positive x
452 static int stringSize(int x) {
453 for (int i=0; ; i++)
454 if (x <= sizeTable[i])
455 return i+1;
456 }
457
458 /**
459 * Parses the string argument as a signed integer in the radix
460 * specified by the second argument. The characters in the string
461 * must all be digits of the specified radix (as determined by
462 * whether {@link java.lang.Character#digit(char, int)} returns a
463 * nonnegative value), except that the first character may be an
464 * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
465 * indicate a negative value or an ASCII plus sign {@code '+'}
466 * ({@code '\u005Cu002B'}) to indicate a positive value. The
467 * resulting integer value is returned.
468 *
469 * <p>An exception of type {@code NumberFormatException} is
470 * thrown if any of the following situations occurs:
471 * <ul>
472 * <li>The first argument is {@code null} or is a string of
473 * length zero.
474 *
475 * <li>The radix is either smaller than
476 * {@link java.lang.Character#MIN_RADIX} or
477 * larger than {@link java.lang.Character#MAX_RADIX}.
478 *
479 * <li>Any character of the string is not a digit of the specified
480 * radix, except that the first character may be a minus sign
481 * {@code '-'} ({@code '\u005Cu002D'}) or plus sign
482 * {@code '+'} ({@code '\u005Cu002B'}) provided that the
483 * string is longer than length 1.
484 *
485 * <li>The value represented by the string is not a value of type
486 * {@code int}.
487 * </ul>
488 *
489 * <p>Examples:
490 * <blockquote><pre>
491 * parseInt("0", 10) returns 0
492 * parseInt("473", 10) returns 473
493 * parseInt("+42", 10) returns 42
494 * parseInt("-0", 10) returns 0
495 * parseInt("-FF", 16) returns -255
496 * parseInt("1100110", 2) returns 102
497 * parseInt("2147483647", 10) returns 2147483647
498 * parseInt("-2147483648", 10) returns -2147483648
499 * parseInt("2147483648", 10) throws a NumberFormatException
500 * parseInt("99", 8) throws a NumberFormatException
501 * parseInt("Kona", 10) throws a NumberFormatException
502 * parseInt("Kona", 27) returns 411787
562 }
563 if (result < multmin) {
564 throw NumberFormatException.forInputString(s);
565 }
566 result *= radix;
567 if (result < limit + digit) {
568 throw NumberFormatException.forInputString(s);
569 }
570 result -= digit;
571 }
572 } else {
573 throw NumberFormatException.forInputString(s);
574 }
575 return negative ? result : -result;
576 }
577
578 /**
579 * Parses the string argument as a signed decimal integer. The
580 * characters in the string must all be decimal digits, except
581 * that the first character may be an ASCII minus sign {@code '-'}
582 * ({@code '\u005Cu002D'}) to indicate a negative value or an
583 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
584 * indicate a positive value. The resulting integer value is
585 * returned, exactly as if the argument and the radix 10 were
586 * given as arguments to the {@link #parseInt(java.lang.String,
587 * int)} method.
588 *
589 * @param s a {@code String} containing the {@code int}
590 * representation to be parsed
591 * @return the integer value represented by the argument in decimal.
592 * @exception NumberFormatException if the string does not contain a
593 * parsable integer.
594 */
595 public static int parseInt(String s) throws NumberFormatException {
596 return parseInt(s,10);
597 }
598
599 /**
600 * Parses the string argument as an unsigned integer in the radix
601 * specified by the second argument. An unsigned integer maps the
602 * values usually associated with negative numbers to positive
603 * numbers larger than {@code MAX_VALUE}.
604 *
605 * The characters in the string must all be digits of the
606 * specified radix (as determined by whether {@link
607 * java.lang.Character#digit(char, int)} returns a nonnegative
608 * value), except that the first character may be an ASCII plus
609 * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
610 * integer value is returned.
611 *
612 * <p>An exception of type {@code NumberFormatException} is
613 * thrown if any of the following situations occurs:
614 * <ul>
615 * <li>The first argument is {@code null} or is a string of
616 * length zero.
617 *
618 * <li>The radix is either smaller than
619 * {@link java.lang.Character#MIN_RADIX} or
620 * larger than {@link java.lang.Character#MAX_RADIX}.
621 *
622 * <li>Any character of the string is not a digit of the specified
623 * radix, except that the first character may be a plus sign
624 * {@code '+'} ({@code '\u005Cu002B'}) provided that the
625 * string is longer than length 1.
626 *
627 * <li>The value represented by the string is larger than the
628 * largest unsigned {@code int}, 2<sup>32</sup>-1.
629 *
630 * </ul>
631 *
632 *
633 * @param s the {@code String} containing the unsigned integer
634 * representation to be parsed
635 * @param radix the radix to be used while parsing {@code s}.
636 * @return the integer represented by the string argument in the
637 * specified radix.
638 * @throws NumberFormatException if the {@code String}
639 * does not contain a parsable {@code int}.
640 * @since 1.8
641 */
642 public static int parseUnsignedInt(String s, int radix)
643 throws NumberFormatException {
644 if (s == null) {
659 } else {
660 long ell = Long.parseLong(s, radix);
661 if ((ell & 0xffff_ffff_0000_0000L) == 0) {
662 return (int) ell;
663 } else {
664 throw new
665 NumberFormatException(String.format("String value %s exceeds " +
666 "range of unsigned int.", s));
667 }
668 }
669 }
670 } else {
671 throw NumberFormatException.forInputString(s);
672 }
673 }
674
675 /**
676 * Parses the string argument as an unsigned decimal integer. The
677 * characters in the string must all be decimal digits, except
678 * that the first character may be an an ASCII plus sign {@code
679 * '+'} ({@code '\u005Cu002B'}). The resulting integer value
680 * is returned, exactly as if the argument and the radix 10 were
681 * given as arguments to the {@link
682 * #parseUnsignedInt(java.lang.String, int)} method.
683 *
684 * @param s a {@code String} containing the unsigned {@code int}
685 * representation to be parsed
686 * @return the unsigned integer value represented by the argument in decimal.
687 * @throws NumberFormatException if the string does not contain a
688 * parsable unsigned integer.
689 * @since 1.8
690 */
691 public static int parseUnsignedInt(String s) throws NumberFormatException {
692 return parseUnsignedInt(s, 10);
693 }
694
695 /**
696 * Returns an {@code Integer} object holding the value
697 * extracted from the specified {@code String} when parsed
698 * with the radix given by the second argument. The first argument
699 * is interpreted as representing a signed integer in the radix
|