Java.lang Package Classes

Java Integer - parseUnsignedInt() Method



The java.lang.Integer.parseUnsignedInt() method is used to parse the string argument as an unsigned integer in the radix specified by the second argument. An unsigned integer maps the values usually associated with negative numbers to positive numbers larger than MAX_VALUE. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII plus sign '+' ('\u002B'). The resulting integer value is returned.

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a plus sign '+' ('\u002B') provided that the string is longer than length 1.
  • The value represented by the string is larger than the largest unsigned int, 232-1.

Syntax

public static int parseUnsignedInt(String s,
                                   int radix)
                            throws NumberFormatException

Parameters

s Specify the String containing the unsigned integer representation to be parsed
radix Specify the radix to be used while parsing s.

Return Value

Returns the integer represented by the string argument in the specified radix.

Exception

Throws NumberFormatException, if the string does not contain a parsable int.

Example:

In the example below, the java.lang.Integer.parseUnsignedInt() method is used to parse the string argument as an unsigned integer in the specified radix.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding unsigned int value
    String x = "100";
    String y = "6F";

    //creating unsigned int value using 
    //radix as 2 (binary)
    int p = Integer.parseUnsignedInt(x, 2);

    //creating unsigned int value using 
    //radix as 16 (hexadecimal)
    int q = Integer.parseUnsignedInt(y, 16);

    //printing the string
    System.out.println("The string x is: " + x); 
    System.out.println("The string y is: " + y); 

    //printing the unsigned int values 
    System.out.println("The int value p is: " + p);   
    System.out.println("The int value q is: " + q);    
  }
}

The output of the above code will be:

The string x is: 100
The string y is: 6F
The int value p is: 4
The int value q is: 111

❮ Java.lang - Integer