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

Print this page

        

@@ -125,11 +125,10 @@
      * @return  a string representation of the argument in the specified radix.
      * @see     java.lang.Character#MAX_RADIX
      * @see     java.lang.Character#MIN_RADIX
      */
     public static String toString(int i, int radix) {
-
         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
             radix = 10;
 
         /* Use the faster version */
         if (radix == 10) {

@@ -156,10 +155,39 @@
 
         return new String(buf, charPos, (33 - charPos));
     }
 
     /**
+     * Returns an unsigned string representation of the first argument
+     * in the radix specified by the second argument.
+     *
+     * <p>If the radix is smaller than {@code Character.MIN_RADIX}
+     * or larger than {@code Character.MAX_RADIX}, then the radix
+     * {@code 10} is used instead.
+     *
+     * <p>Note that since the first argument is treated as an unsigned
+     * value, no leading sign character is printed.
+     *
+     * <p>If the magnitude is zero, it is represented by a single zero
+     * character {@code '0'} (<code>'&#92;u0030'</code>); otherwise,
+     * the first character of the representation of the magnitude will
+     * not be the zero character.
+     *
+     * <p>The characters used as digits and the behavior of radixes
+     * is the same as {@link #toString(int, int) toString}.
+     *
+     * @param   i       an integer to be converted to an unsigned string.
+     * @param   radix   the radix to use in the string representation.
+     * @return  an unsigned string representation of the argument in the specified radix.
+     * @see     #toString(int, int)
+     * @since 1.7
+     */
+    public static String toUnsignedString(int i, int radix) {
+        return Long.toString(toUnsignedLong(i), radix);
+    }
+
+    /**
      * Returns a string representation of the integer argument as an
      * unsigned integer in base&nbsp;16.
      *
      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
      * if the argument is negative; otherwise, it is equal to the

@@ -190,11 +218,11 @@
      * @return  the string representation of the unsigned integer value
      *          represented by the argument in hexadecimal (base&nbsp;16).
      * @since   JDK1.0.2
      */
     public static String toHexString(int i) {
-        return toUnsignedString(i, 4);
+        return toUnsignedString0(i, 4);
     }
 
     /**
      * Returns a string representation of the integer argument as an
      * unsigned integer in base&nbsp;8.

@@ -222,11 +250,11 @@
      * @return  the string representation of the unsigned integer value
      *          represented by the argument in octal (base&nbsp;8).
      * @since   JDK1.0.2
      */
     public static String toOctalString(int i) {
-        return toUnsignedString(i, 3);
+        return toUnsignedString0(i, 3);
     }
 
     /**
      * Returns a string representation of the integer argument as an
      * unsigned integer in base&nbsp;2.

@@ -247,17 +275,17 @@
      * @return  the string representation of the unsigned integer value
      *          represented by the argument in binary (base&nbsp;2).
      * @since   JDK1.0.2
      */
     public static String toBinaryString(int i) {
-        return toUnsignedString(i, 1);
+        return toUnsignedString0(i, 1);
     }
 
     /**
      * Convert the integer to an unsigned number.
      */
-    private static String toUnsignedString(int i, int shift) {
+    private static String toUnsignedString0(int i, int shift) {
         char[] buf = new char[32];
         int charPos = 32;
         int radix = 1 << shift;
         int mask = radix - 1;
         do {

@@ -332,10 +360,27 @@
         getChars(i, size, buf);
         return new String(0, size, buf);
     }
 
     /**
+     * Returns an unsigned string representation of the argument.
+     *
+     * The argument is converted to unsigned decimal representation
+     * and returned as a string exactly as if the argument and radix
+     * 10 were given as arguments to the {@link #toUnsignedString(int,
+     * int)} method.
+     *
+     * @param   i  an integer to be converted to an unsigned string.
+     * @return  an unsigned string representation of the argument.
+     * @see     #toUnsignedString(int, int)
+     * @since 1.7
+     */
+    public static String toUnsignedString(int i) {
+        return Long.toString(toUnsignedLong(i));
+    }
+
+    /**
      * Places characters representing the integer i into the
      * character array buf. The characters are placed into
      * the buffer backwards starting with the least significant
      * digit at the specified index (exclusive), and working
      * backwards from there.

@@ -526,10 +571,99 @@
     public static int parseInt(String s) throws NumberFormatException {
         return parseInt(s,10);
     }
 
     /**
+     * Parses the string argument as an unsigned integer in the radix
+     * specified by the second argument.
+     *
+     * The characters in the string must all be digits of the
+     * specified radix (as determined by whether {@link
+     * java.lang.Character#digit(char, int)} returns a nonnegative
+     * value), except that the first character may be an ASCII plus
+     * sign {@code '+'} (<code>'&#92;u002B'</code>). The resulting
+     * integer value is returned.
+     *
+     * <p>An exception of type {@code NumberFormatException} is
+     * thrown if any of the following situations occurs:
+     * <ul>
+     * <li>The first argument is {@code null} or is a string of
+     * length zero.
+     *
+     * <li>The radix is either smaller than
+     * {@link java.lang.Character#MIN_RADIX} or
+     * larger than {@link java.lang.Character#MAX_RADIX}.
+     *
+     * <li>Any character of the string is not a digit of the specified
+     * radix, except that the first character may be a plus sign
+     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
+     * string is longer than length 1.
+     *
+     * <li>The value represented by the string is larger than the
+     * largest unsigned {@code int}.
+     *
+     * </ul>
+     *
+     *
+     * @param      s   the {@code String} containing the unsigned integer
+     *                  representation to be parsed
+     * @param      radix   the radix to be used while parsing {@code s}.
+     * @return     the integer represented by the string argument in the
+     *             specified radix.
+     * @throws     NumberFormatException if the {@code String}
+     *             does not contain a parsable {@code int}.
+     * @since 1.7
+     */
+    public static int parseUnsignedInt(String s, int radix)
+                throws NumberFormatException {
+        if (s == null)  {
+            throw new NumberFormatException("null");
+        }
+
+        int len = s.length();
+        if (len > 0) {
+            char firstChar = s.charAt(0);
+            if (firstChar == '-') {
+                throw new 
+                    NumberFormatException(String.format("Illegal leading minus sign " +
+                                                       "on unsigned string %s.", s));
+            } else {
+                long ell = Long.parseLong(s, radix);
+                if ((ell & 0xffffffff00000000L) == 0) {
+                    return (int) ell;
+                } else {
+                    throw new
+                        NumberFormatException(String.format("String value %s exceeds " + 
+                                                            "range of unsigned int.", s));
+                }
+            }
+        } else {
+            throw NumberFormatException.forInputString(s);
+        }
+    }
+
+    /**
+     * Parses the string argument as an unsigned decimal integer. The
+     * characters in the string must all be decimal digits, except
+     * that the first character may be an an ASCII plus sign {@code
+     * '+'} (<code>'&#92;u002B'</code>). The resulting integer value
+     * is returned, exactly as if the argument and the radix 10 were
+     * given as arguments to the {@link
+     * #parseUnsignedInt(java.lang.String, int)} method.
+     *
+     * @param s   a {@code String} containing the unsigned {@code int}
+     *            representation to be parsed
+     * @return    the unsigned integer value represented by the argument in decimal.
+     * @throws    NumberFormatException  if the string does not contain a
+     *            parsable unsigned integer.
+     * @since 1.7
+     */
+    public static int parseUnsignedInt(String s) throws NumberFormatException {
+        return parseUnsignedInt(s, 10);
+    }
+
+    /**
      * Returns an {@code Integer} object holding the value
      * extracted from the specified {@code String} when parsed
      * with the radix given by the second argument. The first argument
      * is interpreted as representing a signed integer in the radix
      * specified by the second argument, exactly as if the arguments

@@ -1029,10 +1163,78 @@
      */
     public static int compare(int x, int y) {
         return (x < y) ? -1 : ((x == y) ? 0 : 1);
     }
 
+    /**
+     * Compares two {@code int} values numerically treating the values
+     * as unsigned.
+     *
+     * @param  x the first {@code int} to compare
+     * @param  y the second {@code int} to compare
+     * @return the value {@code 0} if {@code x == y}; a value less
+     *         than {@code 0} if {@code x < y} as unsigned values; and
+     *         a value greater than {@code 0} if {@code x > y} as
+     *         unsigned values
+     * @since 1.7
+     */
+    public static int compareUnsigned(int x, int y) {
+        return Long.compare(toUnsignedLong(x), toUnsignedLong(y));
+    }
+    
+    /**
+     * Converts the argument to a {@code long} by an unsigned
+     * conversion.  In an unsigned conversion to a {@code long}, the
+     * high-order 32 bits of the {@code long} are zero and the
+     * low-order 32 bits are equal to the bits of the integer
+     * argument.
+     *
+     * @return the argument converted to {@code long} by an unsigned
+     *         conversion
+     * @param  x the value to convert to an unsigned {@code long}
+     * @since 1.7
+     */
+    public static long toUnsignedLong(int x) {
+        return ((long) x) & 0xffffffffL;
+    }
+
+    /**
+     * Returns the unsigned quotient of dividing the first argument by
+     * the second where each argument is interpreted as an unsigned
+     * value.
+     *
+     * In other words, return the unsigned value of {@code
+     * (dividend / divisor)}.
+     *
+     * @return the unsigned quotient of the first argument divided by
+     * the second argument
+     * @param dividend the value to be divided
+     * @param divisor the value doing the dividing
+     * @since 1.7
+     */
+    public static int divideUnsigned(int dividend, int divisor) {
+        return (int)(toUnsignedLong(dividend)/toUnsignedLong(divisor));
+    }
+
+    /**
+     * Returns the unsigned remainder from dividing the first argument by
+     * the second where each argument is interpreted as an unsigned
+     * value.
+     *
+     * In other words, return the unsigned value of {@code
+     * (dividend % divisor)}.
+     *
+     * @return the unsigned remainder of the first argument divided by
+     * the second argument
+     * @param dividend the value to be divided
+     * @param divisor the value doing the dividing
+     * @since 1.7
+     */
+    public static int remainderUnsigned(int dividend, int divisor) {
+        return (int)(toUnsignedLong(dividend)%toUnsignedLong(divisor));
+    }
+
 
     // Bit twiddling
 
     /**
      * The number of bits used to represent an {@code int} value in two's