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

Print this page




 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&nbsp;16).
 228      * @see #parseUnsignedInt(String, int)
 229      * @see #toUnsignedString(int, int)

 230      * @since   1.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&nbsp;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&nbsp;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}


 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&nbsp;2).
 298      * @see #parseUnsignedInt(String, int)
 299      * @see #toUnsignedString(int, int)
 300      * @since   1.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 val, int shift) {
 310         // assert shift > 0 && shift <=5 : "Illegal shift value";
 311         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 312         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 313         char[] buf = new char[chars];
 314 
 315         formatUnsignedInt(val, shift, buf, 0, chars);
 316 
 317         // Use special constructor which takes over "buf".
 318         return new String(buf, true);
 319     }
 320 
 321     /**
 322      * Format a long (treated as unsigned) into a character buffer.






















 323      * @param val the unsigned int to format
 324      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 325      * @param buf the character buffer to write to
 326      * @param offset the offset in the destination buffer to start at
 327      * @param len the number of characters to write
 328      * @return the lowest character  location used
 329      */
 330      static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
 331         int charPos = len;
 332         int radix = 1 << shift;
 333         int mask = radix - 1;
 334         do {
 335             buf[offset + --charPos] = Integer.digits[val & mask];
 336             val >>>= shift;
 337         } while (val != 0 && charPos > 0);
 338 
 339         return charPos;
 340     }
 341 
 342     final static char [] DigitTens = {




 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&nbsp;16).
 228      * @see #parseUnsignedInt(String, int)
 229      * @see #toUnsignedString(int, int)
 230      * @see #toHexString(int, int)
 231      * @since   1.0.2
 232      */
 233     public static String toHexString(int i) {
 234         return toUnsignedString0(i, 4);
 235     }
 236 
 237     /**
 238      * Returns a string representation of the integer argument as an
 239      * unsigned integer in base&nbsp;16, padded with leading zeroes if
 240      * necessary.
 241      *
 242      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 243      * if the argument is negative; otherwise, it is equal to the
 244      * argument.  This value is converted to a string of ASCII digits
 245      * in hexadecimal (base&nbsp;16) with possibly appended leading
 246      * zeroes. A minimum amount of zeroes is appended to ensure
 247      * the length of the resulting string is at least {@code minWidth}.
 248      *
 249      * <p>The value of the argument can be recovered from the returned
 250      * string {@code s} by calling {@link
 251      * Integer#parseUnsignedInt(String, int)
 252      * Integer.parseUnsignedInt(s, 16)}.
 253      *
 254      * <p>If the unsigned magnitude is zero, it is represented by a
 255      * {@code minWidth} zero characters {@code '0'} ({@code '\u005Cu0030'}).
 256      * The following characters are used as hexadecimal digits:
 257      *
 258      * <blockquote>
 259      *  {@code 0123456789abcdef}
 260      * </blockquote>
 261      *
 262      * These are the characters {@code '\u005Cu0030'} through
 263      * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
 264      * {@code '\u005Cu0066'}. If uppercase letters are
 265      * desired, the {@link java.lang.String#toUpperCase()} method may
 266      * be called on the result:
 267      *
 268      * <blockquote>
 269      *  {@code Integer.toHexString(n, 6).toUpperCase()}
 270      * </blockquote>
 271      *
 272      * @param   i        an integer to be converted to a string.
 273      * @param   minWidth a minimum required length of the resulting string.
 274      * @return  the string representation of the unsigned integer value
 275      *          represented by the argument in hexadecimal (base&nbsp;16),
 276      *          padded with leading zeroes if necessary.
 277      * @see #parseUnsignedInt(String, int)
 278      * @see #toUnsignedString(int, int)
 279      * @see #toHexString(int)
 280      * @since 1.9
 281      */
 282     public static String toHexString(int i, int minWidth) {
 283         return toUnsignedString0(i, 4, minWidth);
 284     }
 285 
 286 
 287     /**
 288      * Returns a string representation of the integer argument as an
 289      * unsigned integer in base&nbsp;8.
 290      *
 291      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 292      * if the argument is negative; otherwise, it is equal to the
 293      * argument.  This value is converted to a string of ASCII digits
 294      * in octal (base&nbsp;8) with no extra leading {@code 0}s.
 295      *
 296      * <p>The value of the argument can be recovered from the returned
 297      * string {@code s} by calling {@link
 298      * Integer#parseUnsignedInt(String, int)
 299      * Integer.parseUnsignedInt(s, 8)}.
 300      *
 301      * <p>If the unsigned magnitude is zero, it is represented by a
 302      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 303      * otherwise, the first character of the representation of the
 304      * unsigned magnitude will not be the zero character. The
 305      * following characters are used as octal digits:
 306      *
 307      * <blockquote>
 308      * {@code 01234567}


 338      *
 339      * <p>If the unsigned magnitude is zero, it is represented by a
 340      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 341      * otherwise, the first character of the representation of the
 342      * unsigned magnitude will not be the zero character. The
 343      * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
 344      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 345      *
 346      * @param   i   an integer to be converted to a string.
 347      * @return  the string representation of the unsigned integer value
 348      *          represented by the argument in binary (base&nbsp;2).
 349      * @see #parseUnsignedInt(String, int)
 350      * @see #toUnsignedString(int, int)
 351      * @since   1.0.2
 352      */
 353     public static String toBinaryString(int i) {
 354         return toUnsignedString0(i, 1);
 355     }
 356 
 357     /**
 358      * Format an integer (treated as unsigned) into a String.
 359      * @param val the value to format
 360      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 361      */
 362     private static String toUnsignedString0(int val, int shift) {
 363         // assert shift > 0 && shift <=5 : "Illegal shift value";
 364         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 365         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 366         char[] buf = new char[chars];
 367 
 368         formatUnsignedInt(val, shift, buf, 0, chars);
 369 
 370         // Use special constructor which takes over "buf".
 371         return new String(buf, true);
 372     }
 373 
 374     /**
 375      * Format an integer (treated as unsigned) into a String.
 376      * @param val the value to format
 377      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 378      * @param minWidth the minimum width of the produced String
 379      */
 380     private static String toUnsignedString0(int val, int shift, int minWidth) {
 381         // assert shift > 0 && shift <=5 : "Illegal shift value";
 382         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
 383         int magLen = (mag + (shift - 1)) / shift;
 384         int zeroes = Math.max(Math.max(minWidth, 1) - magLen, 0);
 385         char[] buf = new char[magLen + zeroes];
 386 
 387         for (int i = 0; i < zeroes; ++i)
 388             buf[i] = '0';
 389         if (magLen > 0)
 390             formatUnsignedInt(val, shift, buf, zeroes, magLen);
 391 
 392         // Use special constructor which takes over "buf".
 393         return new String(buf, true);
 394     }
 395 
 396     /**
 397      * Format an integer (treated as unsigned) into a character buffer.
 398      * @param val the unsigned int to format
 399      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 400      * @param buf the character buffer to write to
 401      * @param offset the offset in the destination buffer to start at
 402      * @param len the number of characters to write
 403      * @return the lowest character  location used
 404      */
 405      static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
 406         int charPos = len;
 407         int radix = 1 << shift;
 408         int mask = radix - 1;
 409         do {
 410             buf[offset + --charPos] = Integer.digits[val & mask];
 411             val >>>= shift;
 412         } while (val != 0 && charPos > 0);
 413 
 414         return charPos;
 415     }
 416 
 417     final static char [] DigitTens = {