src/share/classes/java/lang/Integer.java
Print this page
@@ -225,18 +225,69 @@
* @param i an integer to be converted to a string.
* @return the string representation of the unsigned integer value
* represented by the argument in hexadecimal (base 16).
* @see #parseUnsignedInt(String, int)
* @see #toUnsignedString(int, int)
+ * @see #toHexString(int, int)
* @since 1.0.2
*/
public static String toHexString(int i) {
return toUnsignedString0(i, 4);
}
/**
* Returns a string representation of the integer argument as an
+ * unsigned integer in base 16, padded with leading zeroes if
+ * necessary.
+ *
+ * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
+ * if the argument is negative; otherwise, it is equal to the
+ * argument. This value is converted to a string of ASCII digits
+ * in hexadecimal (base 16) with possibly appended leading
+ * zeroes. A minimum amount of zeroes is appended to ensure
+ * the length of the resulting string is at least {@code minWidth}.
+ *
+ * <p>The value of the argument can be recovered from the returned
+ * string {@code s} by calling {@link
+ * Integer#parseUnsignedInt(String, int)
+ * Integer.parseUnsignedInt(s, 16)}.
+ *
+ * <p>If the unsigned magnitude is zero, it is represented by a
+ * {@code minWidth} zero characters {@code '0'} ({@code '\u005Cu0030'}).
+ * The following characters are used as hexadecimal digits:
+ *
+ * <blockquote>
+ * {@code 0123456789abcdef}
+ * </blockquote>
+ *
+ * These are the characters {@code '\u005Cu0030'} through
+ * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
+ * {@code '\u005Cu0066'}. If uppercase letters are
+ * desired, the {@link java.lang.String#toUpperCase()} method may
+ * be called on the result:
+ *
+ * <blockquote>
+ * {@code Integer.toHexString(n, 6).toUpperCase()}
+ * </blockquote>
+ *
+ * @param i an integer to be converted to a string.
+ * @param minWidth a minimum required length of the resulting string.
+ * @return the string representation of the unsigned integer value
+ * represented by the argument in hexadecimal (base 16),
+ * padded with leading zeroes if necessary.
+ * @see #parseUnsignedInt(String, int)
+ * @see #toUnsignedString(int, int)
+ * @see #toHexString(int)
+ * @since 1.9
+ */
+ public static String toHexString(int i, int minWidth) {
+ return toUnsignedString0(i, 4, minWidth);
+ }
+
+
+ /**
+ * Returns a string representation of the integer argument as an
* unsigned integer in base 8.
*
* <p>The unsigned integer value is the argument plus 2<sup>32</sup>
* if the argument is negative; otherwise, it is equal to the
* argument. This value is converted to a string of ASCII digits
@@ -302,11 +353,13 @@
public static String toBinaryString(int i) {
return toUnsignedString0(i, 1);
}
/**
- * Convert the integer to an unsigned number.
+ * Format an integer (treated as unsigned) into a String.
+ * @param val the value to format
+ * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
*/
private static String toUnsignedString0(int val, int shift) {
// assert shift > 0 && shift <=5 : "Illegal shift value";
int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
int chars = Math.max(((mag + (shift - 1)) / shift), 1);
@@ -317,11 +370,33 @@
// Use special constructor which takes over "buf".
return new String(buf, true);
}
/**
- * Format a long (treated as unsigned) into a character buffer.
+ * Format an integer (treated as unsigned) into a String.
+ * @param val the value to format
+ * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
+ * @param minWidth the minimum width of the produced String
+ */
+ private static String toUnsignedString0(int val, int shift, int minWidth) {
+ // assert shift > 0 && shift <=5 : "Illegal shift value";
+ int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
+ int magLen = (mag + (shift - 1)) / shift;
+ int zeroes = Math.max(Math.max(minWidth, 1) - magLen, 0);
+ char[] buf = new char[magLen + zeroes];
+
+ for (int i = 0; i < zeroes; ++i)
+ buf[i] = '0';
+ if (magLen > 0)
+ formatUnsignedInt(val, shift, buf, zeroes, magLen);
+
+ // Use special constructor which takes over "buf".
+ return new String(buf, true);
+ }
+
+ /**
+ * Format an integer (treated as unsigned) into a character buffer.
* @param val the unsigned int to format
* @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
* @param buf the character buffer to write to
* @param offset the offset in the destination buffer to start at
* @param len the number of characters to write