64
65 /**
66 * The {@code Class} instance representing the primitive type
67 * {@code long}.
68 *
69 * @since JDK1.1
70 */
71 @SuppressWarnings("unchecked")
72 public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
73
74 /**
75 * Returns a string representation of the first argument in the
76 * radix specified by the second argument.
77 *
78 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
79 * or larger than {@code Character.MAX_RADIX}, then the radix
80 * {@code 10} is used instead.
81 *
82 * <p>If the first argument is negative, the first element of the
83 * result is the ASCII minus sign {@code '-'}
84 * (<code>'\u002d'</code>). If the first argument is not
85 * negative, no sign character appears in the result.
86 *
87 * <p>The remaining characters of the result represent the magnitude
88 * of the first argument. If the magnitude is zero, it is
89 * represented by a single zero character {@code '0'}
90 * (<code>'\u0030'</code>); otherwise, the first character of
91 * the representation of the magnitude will not be the zero
92 * character. The following ASCII characters are used as digits:
93 *
94 * <blockquote>
95 * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
96 * </blockquote>
97 *
98 * These are <code>'\u0030'</code> through
99 * <code>'\u0039'</code> and <code>'\u0061'</code> through
100 * <code>'\u007a'</code>. If {@code radix} is
101 * <var>N</var>, then the first <var>N</var> of these characters
102 * are used as radix-<var>N</var> digits in the order shown. Thus,
103 * the digits for hexadecimal (radix 16) are
104 * {@code 0123456789abcdef}. If uppercase letters are
105 * desired, the {@link java.lang.String#toUpperCase()} method may
106 * be called on the result:
107 *
108 * <blockquote>
109 * {@code Long.toString(n, 16).toUpperCase()}
110 * </blockquote>
111 *
112 * @param i a {@code long} to be converted to a string.
113 * @param radix the radix to use in the string representation.
114 * @return a string representation of the argument in the specified radix.
115 * @see java.lang.Character#MAX_RADIX
116 * @see java.lang.Character#MIN_RADIX
117 */
118 public static String toString(long i, int radix) {
119 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
120 radix = 10;
137 if (negative) {
138 buf[--charPos] = '-';
139 }
140
141 return new String(buf, charPos, (65 - charPos));
142 }
143
144 /**
145 * Returns a string representation of the first argument as an
146 * unsigned integer value in the radix specified by the second
147 * argument.
148 *
149 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
150 * or larger than {@code Character.MAX_RADIX}, then the radix
151 * {@code 10} is used instead.
152 *
153 * <p>Note that since the first argument is treated as an unsigned
154 * value, no leading sign character is printed.
155 *
156 * <p>If the magnitude is zero, it is represented by a single zero
157 * character {@code '0'} (<code>'\u0030'</code>); otherwise,
158 * the first character of the representation of the magnitude will
159 * not be the zero character.
160 *
161 * <p>The behavior of radixes and the characters used as digits
162 * are the same as {@link #toString(long, int) toString}.
163 *
164 * @param i an integer to be converted to an unsigned string.
165 * @param radix the radix to use in the string representation.
166 * @return an unsigned string representation of the argument in the specified radix.
167 * @see #toString(long, int)
168 * @since 1.8
169 */
170 public static String toUnsignedString(long i, int radix) {
171 if (i >= 0)
172 return toString(i, radix);
173 else {
174 switch (radix) {
175 case 2:
176 return toBinaryString(i);
177
222 add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
223 }
224 }
225
226 /**
227 * Returns a string representation of the {@code long}
228 * argument as an unsigned integer in base 16.
229 *
230 * <p>The unsigned {@code long} value is the argument plus
231 * 2<sup>64</sup> if the argument is negative; otherwise, it is
232 * equal to the argument. This value is converted to a string of
233 * ASCII digits in hexadecimal (base 16) with no extra
234 * leading {@code 0}s.
235 *
236 * <p>The value of the argument can be recovered from the returned
237 * string {@code s} by calling {@link
238 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
239 * 16)}.
240 *
241 * <p>If the unsigned magnitude is zero, it is represented by a
242 * single zero character {@code '0'} (<code>'\u0030'</code>);
243 * otherwise, the first character of the representation of the
244 * unsigned magnitude will not be the zero character. The
245 * following characters are used as hexadecimal digits:
246 *
247 * <blockquote>
248 * {@code 0123456789abcdef}
249 * </blockquote>
250 *
251 * These are the characters <code>'\u0030'</code> through
252 * <code>'\u0039'</code> and <code>'\u0061'</code> through
253 * <code>'\u0066'</code>. If uppercase letters are desired,
254 * the {@link java.lang.String#toUpperCase()} method may be called
255 * on the result:
256 *
257 * <blockquote>
258 * {@code Long.toHexString(n).toUpperCase()}
259 * </blockquote>
260 *
261 * @param i a {@code long} to be converted to a string.
262 * @return the string representation of the unsigned {@code long}
263 * value represented by the argument in hexadecimal
264 * (base 16).
265 * @see #parseUnsignedLong(String, int)
266 * @see #toUnsignedString(long, int)
267 * @since JDK 1.0.2
268 */
269 public static String toHexString(long i) {
270 return toUnsignedString0(i, 4);
271 }
272
273 /**
274 * Returns a string representation of the {@code long}
275 * argument as an unsigned integer in base 8.
276 *
277 * <p>The unsigned {@code long} value is the argument plus
278 * 2<sup>64</sup> if the argument is negative; otherwise, it is
279 * equal to the argument. This value is converted to a string of
280 * ASCII digits in octal (base 8) with no extra leading
281 * {@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 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
286 * 8)}.
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 * following characters are used as octal digits:
293 *
294 * <blockquote>
295 * {@code 01234567}
296 * </blockquote>
297 *
298 * These are the characters <code>'\u0030'</code> through
299 * <code>'\u0037'</code>.
300 *
301 * @param i a {@code long} to be converted to a string.
302 * @return the string representation of the unsigned {@code long}
303 * value represented by the argument in octal (base 8).
304 * @see #parseUnsignedLong(String, int)
305 * @see #toUnsignedString(long, int)
306 * @since JDK 1.0.2
307 */
308 public static String toOctalString(long i) {
309 return toUnsignedString0(i, 3);
310 }
311
312 /**
313 * Returns a string representation of the {@code long}
314 * argument as an unsigned integer in base 2.
315 *
316 * <p>The unsigned {@code long} value is the argument plus
317 * 2<sup>64</sup> if the argument is negative; otherwise, it is
318 * equal to the argument. This value is converted to a string of
319 * ASCII digits in binary (base 2) with no extra leading
320 * {@code 0}s.
321 *
322 * <p>The value of the argument can be recovered from the returned
323 * string {@code s} by calling {@link
324 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
325 * 2)}.
326 *
327 * <p>If the unsigned magnitude is zero, it is represented by a
328 * single zero character {@code '0'} (<code>'\u0030'</code>);
329 * otherwise, the first character of the representation of the
330 * unsigned magnitude will not be the zero character. The
331 * characters {@code '0'} (<code>'\u0030'</code>) and {@code
332 * '1'} (<code>'\u0031'</code>) are used as binary digits.
333 *
334 * @param i a {@code long} to be converted to a string.
335 * @return the string representation of the unsigned {@code long}
336 * value represented by the argument in binary (base 2).
337 * @see #parseUnsignedLong(String, int)
338 * @see #toUnsignedString(long, int)
339 * @since JDK 1.0.2
340 */
341 public static String toBinaryString(long i) {
342 return toUnsignedString0(i, 1);
343 }
344
345 /**
346 * Convert the integer to an unsigned number.
347 */
348 private static String toUnsignedString0(long i, int shift) {
349 char[] buf = new char[64];
350 int charPos = 64;
351 int radix = 1 << shift;
352 long mask = radix - 1;
450 }
451 }
452
453 // Requires positive x
454 static int stringSize(long x) {
455 long p = 10;
456 for (int i=1; i<19; i++) {
457 if (x < p)
458 return i;
459 p = 10*p;
460 }
461 return 19;
462 }
463
464 /**
465 * Parses the string argument as a signed {@code long} in the
466 * radix specified by the second argument. The characters in the
467 * string must all be digits of the specified radix (as determined
468 * by whether {@link java.lang.Character#digit(char, int)} returns
469 * a nonnegative value), except that the first character may be an
470 * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
471 * indicate a negative value or an ASCII plus sign {@code '+'}
472 * (<code>'\u002B'</code>) to indicate a positive value. The
473 * resulting {@code long} value is returned.
474 *
475 * <p>Note that neither the character {@code L}
476 * (<code>'\u004C'</code>) nor {@code l}
477 * (<code>'\u006C'</code>) is permitted to appear at the end
478 * of the string as a type indicator, as would be permitted in
479 * Java programming language source code - except that either
480 * {@code L} or {@code l} may appear as a digit for a
481 * radix greater than 22.
482 *
483 * <p>An exception of type {@code NumberFormatException} is
484 * thrown if any of the following situations occurs:
485 * <ul>
486 *
487 * <li>The first argument is {@code null} or is a string of
488 * length zero.
489 *
490 * <li>The {@code radix} is either smaller than {@link
491 * java.lang.Character#MIN_RADIX} or larger than {@link
492 * java.lang.Character#MAX_RADIX}.
493 *
494 * <li>Any character of the string is not a digit of the specified
495 * radix, except that the first character may be a minus sign
496 * {@code '-'} (<code>'\u002d'</code>) or plus sign {@code
497 * '+'} (<code>'\u002B'</code>) provided that the string is
498 * longer than length 1.
499 *
500 * <li>The value represented by the string is not a value of type
501 * {@code long}.
502 * </ul>
503 *
504 * <p>Examples:
505 * <blockquote><pre>
506 * parseLong("0", 10) returns 0L
507 * parseLong("473", 10) returns 473L
508 * parseLong("+42", 10) returns 42L
509 * parseLong("-0", 10) returns 0L
510 * parseLong("-FF", 16) returns -255L
511 * parseLong("1100110", 2) returns 102L
512 * parseLong("99", 8) throws a NumberFormatException
513 * parseLong("Hazelnut", 10) throws a NumberFormatException
514 * parseLong("Hazelnut", 36) returns 1356099454469L
515 * </pre></blockquote>
516 *
517 * @param s the {@code String} containing the
567 }
568 if (result < multmin) {
569 throw NumberFormatException.forInputString(s);
570 }
571 result *= radix;
572 if (result < limit + digit) {
573 throw NumberFormatException.forInputString(s);
574 }
575 result -= digit;
576 }
577 } else {
578 throw NumberFormatException.forInputString(s);
579 }
580 return negative ? result : -result;
581 }
582
583 /**
584 * Parses the string argument as a signed decimal {@code long}.
585 * The characters in the string must all be decimal digits, except
586 * that the first character may be an ASCII minus sign {@code '-'}
587 * (<code>\u002D'</code>) to indicate a negative value or an
588 * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
589 * indicate a positive value. The resulting {@code long} value is
590 * returned, exactly as if the argument and the radix {@code 10}
591 * were given as arguments to the {@link
592 * #parseLong(java.lang.String, int)} method.
593 *
594 * <p>Note that neither the character {@code L}
595 * (<code>'\u004C'</code>) nor {@code l}
596 * (<code>'\u006C'</code>) is permitted to appear at the end
597 * of the string as a type indicator, as would be permitted in
598 * Java programming language source code.
599 *
600 * @param s a {@code String} containing the {@code long}
601 * representation to be parsed
602 * @return the {@code long} represented by the argument in
603 * decimal.
604 * @throws NumberFormatException if the string does not contain a
605 * parsable {@code long}.
606 */
607 public static long parseLong(String s) throws NumberFormatException {
608 return parseLong(s, 10);
609 }
610
611 /**
612 * Parses the string argument as an unsigned {@code long} in the
613 * radix specified by the second argument. An unsigned integer
614 * maps the values usually associated with negative numbers to
615 * positive numbers larger than {@code MAX_VALUE}.
616 *
617 * The characters in the string must all be digits of the
618 * specified radix (as determined by whether {@link
619 * java.lang.Character#digit(char, int)} returns a nonnegative
620 * value), except that the first character may be an ASCII plus
621 * sign {@code '+'} (<code>'\u002B'</code>). The resulting
622 * integer value is returned.
623 *
624 * <p>An exception of type {@code NumberFormatException} is
625 * thrown if any of the following situations occurs:
626 * <ul>
627 * <li>The first argument is {@code null} or is a string of
628 * length zero.
629 *
630 * <li>The radix is either smaller than
631 * {@link java.lang.Character#MIN_RADIX} or
632 * larger than {@link java.lang.Character#MAX_RADIX}.
633 *
634 * <li>Any character of the string is not a digit of the specified
635 * radix, except that the first character may be a plus sign
636 * {@code '+'} (<code>'\u002B'</code>) provided that the
637 * string is longer than length 1.
638 *
639 * <li>The value represented by the string is larger than the
640 * largest unsigned {@code long}, 2<sup>64</sup>-1.
641 *
642 * </ul>
643 *
644 *
645 * @param s the {@code String} containing the unsigned integer
646 * representation to be parsed
647 * @param radix the radix to be used while parsing {@code s}.
648 * @return the unsigned {@code long} represented by the string
649 * argument in the specified radix.
650 * @throws NumberFormatException if the {@code String}
651 * does not contain a parsable {@code long}.
652 * @since 1.8
653 */
654 public static long parseUnsignedLong(String s, int radix)
655 throws NumberFormatException {
656 if (s == null) {
690 *
691 * The compareUnsigned check above catches
692 * situations where an unsigned overflow occurs
693 * incorporating the contribution of the final
694 * digit.
695 */
696 throw new NumberFormatException(String.format("String value %s exceeds " +
697 "range of unsigned long.", s));
698 }
699 return result;
700 }
701 } else {
702 throw NumberFormatException.forInputString(s);
703 }
704 }
705
706 /**
707 * Parses the string argument as an unsigned decimal {@code long}. The
708 * characters in the string must all be decimal digits, except
709 * that the first character may be an an ASCII plus sign {@code
710 * '+'} (<code>'\u002B'</code>). The resulting integer value
711 * is returned, exactly as if the argument and the radix 10 were
712 * given as arguments to the {@link
713 * #parseUnsignedLong(java.lang.String, int)} method.
714 *
715 * @param s a {@code String} containing the unsigned {@code long}
716 * representation to be parsed
717 * @return the unsigned {@code long} value represented by the decimal string argument
718 * @throws NumberFormatException if the string does not contain a
719 * parsable unsigned integer.
720 * @since 1.8
721 */
722 public static long parseUnsignedLong(String s) throws NumberFormatException {
723 return parseUnsignedLong(s, 10);
724 }
725
726 /**
727 * Returns a {@code Long} object holding the value
728 * extracted from the specified {@code String} when parsed
729 * with the radix given by the second argument. The first
730 * argument is interpreted as representing a signed
1131 * as a {@code long} value, as per the
1132 * {@link Long#decode decode} method, and a {@code Long} object
1133 * representing this value is returned; in summary:
1134 *
1135 * <ul>
1136 * <li>If the property value begins with the two ASCII characters
1137 * {@code 0x} or the ASCII character {@code #}, not followed by
1138 * a minus sign, then the rest of it is parsed as a hexadecimal integer
1139 * exactly as for the method {@link #valueOf(java.lang.String, int)}
1140 * with radix 16.
1141 * <li>If the property value begins with the ASCII character
1142 * {@code 0} followed by another character, it is parsed as
1143 * an octal integer exactly as by the method {@link
1144 * #valueOf(java.lang.String, int)} with radix 8.
1145 * <li>Otherwise the property value is parsed as a decimal
1146 * integer exactly as by the method
1147 * {@link #valueOf(java.lang.String, int)} with radix 10.
1148 * </ul>
1149 *
1150 * <p>Note that, in every case, neither {@code L}
1151 * (<code>'\u004C'</code>) nor {@code l}
1152 * (<code>'\u006C'</code>) is permitted to appear at the end
1153 * of the property value as a type indicator, as would be
1154 * permitted in Java programming language source code.
1155 *
1156 * <p>The second argument is the default value. The default value is
1157 * returned if there is no property of the specified name, if the
1158 * property does not have the correct numeric format, or if the
1159 * specified name is empty or {@code null}.
1160 *
1161 * @param nm property name.
1162 * @param val default value.
1163 * @return the {@code Long} value of the property.
1164 * @throws SecurityException for the same reasons as
1165 * {@link System#getProperty(String) System.getProperty}
1166 * @see System#getProperty(java.lang.String)
1167 * @see System#getProperty(java.lang.String, java.lang.String)
1168 */
1169 public static Long getLong(String nm, Long val) {
1170 String v = null;
1171 try {
1172 v = System.getProperty(nm);
|
64
65 /**
66 * The {@code Class} instance representing the primitive type
67 * {@code long}.
68 *
69 * @since JDK1.1
70 */
71 @SuppressWarnings("unchecked")
72 public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
73
74 /**
75 * Returns a string representation of the first argument in the
76 * radix specified by the second argument.
77 *
78 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
79 * or larger than {@code Character.MAX_RADIX}, then the radix
80 * {@code 10} is used instead.
81 *
82 * <p>If the first argument is negative, the first element of the
83 * result is the ASCII minus sign {@code '-'}
84 * ({@code '\u005Cu002d'}). If the first argument is not
85 * negative, no sign character appears in the result.
86 *
87 * <p>The remaining characters of the result represent the magnitude
88 * of the first argument. If the magnitude is zero, it is
89 * represented by a single zero character {@code '0'}
90 * ({@code '\u005Cu0030'}); otherwise, the first character of
91 * the representation of the magnitude will not be the zero
92 * character. The following ASCII characters are used as digits:
93 *
94 * <blockquote>
95 * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
96 * </blockquote>
97 *
98 * These are {@code '\u005Cu0030'} through
99 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
100 * {@code '\u005Cu007a'}. If {@code radix} is
101 * <var>N</var>, then the first <var>N</var> of these characters
102 * are used as radix-<var>N</var> digits in the order shown. Thus,
103 * the digits for hexadecimal (radix 16) are
104 * {@code 0123456789abcdef}. If uppercase letters are
105 * desired, the {@link java.lang.String#toUpperCase()} method may
106 * be called on the result:
107 *
108 * <blockquote>
109 * {@code Long.toString(n, 16).toUpperCase()}
110 * </blockquote>
111 *
112 * @param i a {@code long} to be converted to a string.
113 * @param radix the radix to use in the string representation.
114 * @return a string representation of the argument in the specified radix.
115 * @see java.lang.Character#MAX_RADIX
116 * @see java.lang.Character#MIN_RADIX
117 */
118 public static String toString(long i, int radix) {
119 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
120 radix = 10;
137 if (negative) {
138 buf[--charPos] = '-';
139 }
140
141 return new String(buf, charPos, (65 - charPos));
142 }
143
144 /**
145 * Returns a string representation of the first argument as an
146 * unsigned integer value in the radix specified by the second
147 * argument.
148 *
149 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
150 * or larger than {@code Character.MAX_RADIX}, then the radix
151 * {@code 10} is used instead.
152 *
153 * <p>Note that since the first argument is treated as an unsigned
154 * value, no leading sign character is printed.
155 *
156 * <p>If the magnitude is zero, it is represented by a single zero
157 * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
158 * the first character of the representation of the magnitude will
159 * not be the zero character.
160 *
161 * <p>The behavior of radixes and the characters used as digits
162 * are the same as {@link #toString(long, int) toString}.
163 *
164 * @param i an integer to be converted to an unsigned string.
165 * @param radix the radix to use in the string representation.
166 * @return an unsigned string representation of the argument in the specified radix.
167 * @see #toString(long, int)
168 * @since 1.8
169 */
170 public static String toUnsignedString(long i, int radix) {
171 if (i >= 0)
172 return toString(i, radix);
173 else {
174 switch (radix) {
175 case 2:
176 return toBinaryString(i);
177
222 add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
223 }
224 }
225
226 /**
227 * Returns a string representation of the {@code long}
228 * argument as an unsigned integer in base 16.
229 *
230 * <p>The unsigned {@code long} value is the argument plus
231 * 2<sup>64</sup> if the argument is negative; otherwise, it is
232 * equal to the argument. This value is converted to a string of
233 * ASCII digits in hexadecimal (base 16) with no extra
234 * leading {@code 0}s.
235 *
236 * <p>The value of the argument can be recovered from the returned
237 * string {@code s} by calling {@link
238 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
239 * 16)}.
240 *
241 * <p>If the unsigned magnitude is zero, it is represented by a
242 * single zero character {@code '0'} ({@code '\u005Cu0030'});
243 * otherwise, the first character of the representation of the
244 * unsigned magnitude will not be the zero character. The
245 * following characters are used as hexadecimal digits:
246 *
247 * <blockquote>
248 * {@code 0123456789abcdef}
249 * </blockquote>
250 *
251 * These are the characters {@code '\u005Cu0030'} through
252 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
253 * {@code '\u005Cu0066'}. If uppercase letters are desired,
254 * the {@link java.lang.String#toUpperCase()} method may be called
255 * on the result:
256 *
257 * <blockquote>
258 * {@code Long.toHexString(n).toUpperCase()}
259 * </blockquote>
260 *
261 * @param i a {@code long} to be converted to a string.
262 * @return the string representation of the unsigned {@code long}
263 * value represented by the argument in hexadecimal
264 * (base 16).
265 * @see #parseUnsignedLong(String, int)
266 * @see #toUnsignedString(long, int)
267 * @since JDK 1.0.2
268 */
269 public static String toHexString(long i) {
270 return toUnsignedString0(i, 4);
271 }
272
273 /**
274 * Returns a string representation of the {@code long}
275 * argument as an unsigned integer in base 8.
276 *
277 * <p>The unsigned {@code long} value is the argument plus
278 * 2<sup>64</sup> if the argument is negative; otherwise, it is
279 * equal to the argument. This value is converted to a string of
280 * ASCII digits in octal (base 8) with no extra leading
281 * {@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 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
286 * 8)}.
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 * following characters are used as octal digits:
293 *
294 * <blockquote>
295 * {@code 01234567}
296 * </blockquote>
297 *
298 * These are the characters {@code '\u005Cu0030'} through
299 * {@code '\u005Cu0037'}.
300 *
301 * @param i a {@code long} to be converted to a string.
302 * @return the string representation of the unsigned {@code long}
303 * value represented by the argument in octal (base 8).
304 * @see #parseUnsignedLong(String, int)
305 * @see #toUnsignedString(long, int)
306 * @since JDK 1.0.2
307 */
308 public static String toOctalString(long i) {
309 return toUnsignedString0(i, 3);
310 }
311
312 /**
313 * Returns a string representation of the {@code long}
314 * argument as an unsigned integer in base 2.
315 *
316 * <p>The unsigned {@code long} value is the argument plus
317 * 2<sup>64</sup> if the argument is negative; otherwise, it is
318 * equal to the argument. This value is converted to a string of
319 * ASCII digits in binary (base 2) with no extra leading
320 * {@code 0}s.
321 *
322 * <p>The value of the argument can be recovered from the returned
323 * string {@code s} by calling {@link
324 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
325 * 2)}.
326 *
327 * <p>If the unsigned magnitude is zero, it is represented by a
328 * single zero character {@code '0'} ({@code '\u005Cu0030'});
329 * otherwise, the first character of the representation of the
330 * unsigned magnitude will not be the zero character. The
331 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
332 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
333 *
334 * @param i a {@code long} to be converted to a string.
335 * @return the string representation of the unsigned {@code long}
336 * value represented by the argument in binary (base 2).
337 * @see #parseUnsignedLong(String, int)
338 * @see #toUnsignedString(long, int)
339 * @since JDK 1.0.2
340 */
341 public static String toBinaryString(long i) {
342 return toUnsignedString0(i, 1);
343 }
344
345 /**
346 * Convert the integer to an unsigned number.
347 */
348 private static String toUnsignedString0(long i, int shift) {
349 char[] buf = new char[64];
350 int charPos = 64;
351 int radix = 1 << shift;
352 long mask = radix - 1;
450 }
451 }
452
453 // Requires positive x
454 static int stringSize(long x) {
455 long p = 10;
456 for (int i=1; i<19; i++) {
457 if (x < p)
458 return i;
459 p = 10*p;
460 }
461 return 19;
462 }
463
464 /**
465 * Parses the string argument as a signed {@code long} in the
466 * radix specified by the second argument. The characters in the
467 * string must all be digits of the specified radix (as determined
468 * by whether {@link java.lang.Character#digit(char, int)} returns
469 * a nonnegative value), except that the first character may be an
470 * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
471 * indicate a negative value or an ASCII plus sign {@code '+'}
472 * ({@code '\u005Cu002B'}) to indicate a positive value. The
473 * resulting {@code long} value is returned.
474 *
475 * <p>Note that neither the character {@code L}
476 * ({@code '\u005Cu004C'}) nor {@code l}
477 * ({@code '\u005Cu006C'}) is permitted to appear at the end
478 * of the string as a type indicator, as would be permitted in
479 * Java programming language source code - except that either
480 * {@code L} or {@code l} may appear as a digit for a
481 * radix greater than 22.
482 *
483 * <p>An exception of type {@code NumberFormatException} is
484 * thrown if any of the following situations occurs:
485 * <ul>
486 *
487 * <li>The first argument is {@code null} or is a string of
488 * length zero.
489 *
490 * <li>The {@code radix} is either smaller than {@link
491 * java.lang.Character#MIN_RADIX} or larger than {@link
492 * java.lang.Character#MAX_RADIX}.
493 *
494 * <li>Any character of the string is not a digit of the specified
495 * radix, except that the first character may be a minus sign
496 * {@code '-'} ({@code '\u005Cu002d'}) or plus sign {@code
497 * '+'} ({@code '\u005Cu002B'}) provided that the string is
498 * longer than length 1.
499 *
500 * <li>The value represented by the string is not a value of type
501 * {@code long}.
502 * </ul>
503 *
504 * <p>Examples:
505 * <blockquote><pre>
506 * parseLong("0", 10) returns 0L
507 * parseLong("473", 10) returns 473L
508 * parseLong("+42", 10) returns 42L
509 * parseLong("-0", 10) returns 0L
510 * parseLong("-FF", 16) returns -255L
511 * parseLong("1100110", 2) returns 102L
512 * parseLong("99", 8) throws a NumberFormatException
513 * parseLong("Hazelnut", 10) throws a NumberFormatException
514 * parseLong("Hazelnut", 36) returns 1356099454469L
515 * </pre></blockquote>
516 *
517 * @param s the {@code String} containing the
567 }
568 if (result < multmin) {
569 throw NumberFormatException.forInputString(s);
570 }
571 result *= radix;
572 if (result < limit + digit) {
573 throw NumberFormatException.forInputString(s);
574 }
575 result -= digit;
576 }
577 } else {
578 throw NumberFormatException.forInputString(s);
579 }
580 return negative ? result : -result;
581 }
582
583 /**
584 * Parses the string argument as a signed decimal {@code long}.
585 * The characters in the string must all be decimal digits, except
586 * that the first character may be an ASCII minus sign {@code '-'}
587 * ({@code \u005Cu002D'}) to indicate a negative value or an
588 * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
589 * indicate a positive value. The resulting {@code long} value is
590 * returned, exactly as if the argument and the radix {@code 10}
591 * were given as arguments to the {@link
592 * #parseLong(java.lang.String, int)} method.
593 *
594 * <p>Note that neither the character {@code L}
595 * ({@code '\u005Cu004C'}) nor {@code l}
596 * ({@code '\u005Cu006C'}) is permitted to appear at the end
597 * of the string as a type indicator, as would be permitted in
598 * Java programming language source code.
599 *
600 * @param s a {@code String} containing the {@code long}
601 * representation to be parsed
602 * @return the {@code long} represented by the argument in
603 * decimal.
604 * @throws NumberFormatException if the string does not contain a
605 * parsable {@code long}.
606 */
607 public static long parseLong(String s) throws NumberFormatException {
608 return parseLong(s, 10);
609 }
610
611 /**
612 * Parses the string argument as an unsigned {@code long} in the
613 * radix specified by the second argument. An unsigned integer
614 * maps the values usually associated with negative numbers to
615 * positive numbers larger than {@code MAX_VALUE}.
616 *
617 * The characters in the string must all be digits of the
618 * specified radix (as determined by whether {@link
619 * java.lang.Character#digit(char, int)} returns a nonnegative
620 * value), except that the first character may be an ASCII plus
621 * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
622 * integer value is returned.
623 *
624 * <p>An exception of type {@code NumberFormatException} is
625 * thrown if any of the following situations occurs:
626 * <ul>
627 * <li>The first argument is {@code null} or is a string of
628 * length zero.
629 *
630 * <li>The radix is either smaller than
631 * {@link java.lang.Character#MIN_RADIX} or
632 * larger than {@link java.lang.Character#MAX_RADIX}.
633 *
634 * <li>Any character of the string is not a digit of the specified
635 * radix, except that the first character may be a plus sign
636 * {@code '+'} ({@code '\u005Cu002B'}) provided that the
637 * string is longer than length 1.
638 *
639 * <li>The value represented by the string is larger than the
640 * largest unsigned {@code long}, 2<sup>64</sup>-1.
641 *
642 * </ul>
643 *
644 *
645 * @param s the {@code String} containing the unsigned integer
646 * representation to be parsed
647 * @param radix the radix to be used while parsing {@code s}.
648 * @return the unsigned {@code long} represented by the string
649 * argument in the specified radix.
650 * @throws NumberFormatException if the {@code String}
651 * does not contain a parsable {@code long}.
652 * @since 1.8
653 */
654 public static long parseUnsignedLong(String s, int radix)
655 throws NumberFormatException {
656 if (s == null) {
690 *
691 * The compareUnsigned check above catches
692 * situations where an unsigned overflow occurs
693 * incorporating the contribution of the final
694 * digit.
695 */
696 throw new NumberFormatException(String.format("String value %s exceeds " +
697 "range of unsigned long.", s));
698 }
699 return result;
700 }
701 } else {
702 throw NumberFormatException.forInputString(s);
703 }
704 }
705
706 /**
707 * Parses the string argument as an unsigned decimal {@code long}. The
708 * characters in the string must all be decimal digits, except
709 * that the first character may be an an ASCII plus sign {@code
710 * '+'} ({@code '\u005Cu002B'}). The resulting integer value
711 * is returned, exactly as if the argument and the radix 10 were
712 * given as arguments to the {@link
713 * #parseUnsignedLong(java.lang.String, int)} method.
714 *
715 * @param s a {@code String} containing the unsigned {@code long}
716 * representation to be parsed
717 * @return the unsigned {@code long} value represented by the decimal string argument
718 * @throws NumberFormatException if the string does not contain a
719 * parsable unsigned integer.
720 * @since 1.8
721 */
722 public static long parseUnsignedLong(String s) throws NumberFormatException {
723 return parseUnsignedLong(s, 10);
724 }
725
726 /**
727 * Returns a {@code Long} object holding the value
728 * extracted from the specified {@code String} when parsed
729 * with the radix given by the second argument. The first
730 * argument is interpreted as representing a signed
1131 * as a {@code long} value, as per the
1132 * {@link Long#decode decode} method, and a {@code Long} object
1133 * representing this value is returned; in summary:
1134 *
1135 * <ul>
1136 * <li>If the property value begins with the two ASCII characters
1137 * {@code 0x} or the ASCII character {@code #}, not followed by
1138 * a minus sign, then the rest of it is parsed as a hexadecimal integer
1139 * exactly as for the method {@link #valueOf(java.lang.String, int)}
1140 * with radix 16.
1141 * <li>If the property value begins with the ASCII character
1142 * {@code 0} followed by another character, it is parsed as
1143 * an octal integer exactly as by the method {@link
1144 * #valueOf(java.lang.String, int)} with radix 8.
1145 * <li>Otherwise the property value is parsed as a decimal
1146 * integer exactly as by the method
1147 * {@link #valueOf(java.lang.String, int)} with radix 10.
1148 * </ul>
1149 *
1150 * <p>Note that, in every case, neither {@code L}
1151 * ({@code '\u005Cu004C'}) nor {@code l}
1152 * ({@code '\u005Cu006C'}) is permitted to appear at the end
1153 * of the property value as a type indicator, as would be
1154 * permitted in Java programming language source code.
1155 *
1156 * <p>The second argument is the default value. The default value is
1157 * returned if there is no property of the specified name, if the
1158 * property does not have the correct numeric format, or if the
1159 * specified name is empty or {@code null}.
1160 *
1161 * @param nm property name.
1162 * @param val default value.
1163 * @return the {@code Long} value of the property.
1164 * @throws SecurityException for the same reasons as
1165 * {@link System#getProperty(String) System.getProperty}
1166 * @see System#getProperty(java.lang.String)
1167 * @see System#getProperty(java.lang.String, java.lang.String)
1168 */
1169 public static Long getLong(String nm, Long val) {
1170 String v = null;
1171 try {
1172 v = System.getProperty(nm);
|