Signup/Sign In

Java Integer toUnsignedString(int i, int radix) Method

Java toUnsignedString() method is a part of the Integer class of the java.lang package. This method is used to return the unsigned String equivalent of the integer value passed as argument in the base(radix) passed.

For example, toUnsignedString(65,2) will return 1000001 because the value of 65 as unsigned value in base 2 is 1000001 while toUnsignedString(-65,2) will return 11111111111111111111111110111111 as the value of -65 as an unsigned value in base 2 is 11111111111111111111111110111111.

It must be noted that if the radix value is less than Character.MIN_RADIX or more than Character.MAX_RADIX then base 10 is used.

Syntax:

public static String toUnsignedString(int i, int radix)  

Parameters:

The parameters passed are int i whose unsigned String equivalent is to be returned and int radix which defines the base for the String conversion.

Returns:

Returns the unsigned String equivalent of the integer value passed in accordance with the radix value passed as the parameter.

Example 1:

Here, the integer values are converted into its equivalent unsigned String representations in accordance with the radix value.

import java.lang.Integer;

public class StudyTonight
{  
    public static void main(String[] args)
     {  
        int a = -78;
        int b = 78;
        int d = 10;
        int h = 16;
        int o = 8;
          
        String s1 = Integer.toUnsignedString(a,d); //Returns the unsigned string representation of the integer value with radix 10  
        System.out.println("Equivalent String Value = " + s1);   
       
        String s2 = Integer.toUnsignedString(a,h);  //Returns the unsigned string representation of the integer value with radix 16  
        System.out.println("Equivalent String Value = " + s2);  
        
        String s3 = Integer.toUnsignedString(a, o); //Returns the unsigned string representation of the integer value with radix 8   
        System.out.println("Equivalent String Value = " + s3);  
          
        String s4 = Integer.toUnsignedString(b, d);  //Returns the unsigned string representation of the integer value with radix 10  
        System.out.println("Equivalent String Value = " + s4);  
        
        String s5 = Integer.toUnsignedString(b, h);  //Returns the unsigned string representation of the integer value with radix 16  
        System.out.println("Equivalent String Value = " + s5);  
        
        String s6 = Integer.toUnsignedString(b, o); //Returns the unsigned string representation of the integer value with radix 8   
        System.out.println("Equivalent String Value = " + s6);  
        
     }  
}


Equivalent String Value = -78
Equivalent String Value = -4e
Equivalent String Value = -116
Equivalent String Value = 78
Equivalent String Value = 4e
Equivalent String Value = 116

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  
public class StudyTonight 
{  
    public static void main(String[] args) 
    {            
        try
        {
           System.out.println("Enter the value and base ");  
           Scanner sc = new Scanner(System.in);  
           int val = sc.nextInt();  
           int b = sc.nextInt();  
           System.out.println("Output: "+Integer.toUnsignedString(val, b));  //returns string with equivalent base
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }
    }  
} 


Enter the value and base
68 8
Output: 104
*******************************
Enter the value and base
-82 16
Output: ffffffae
******************************
Enter the value and base
0x389 8
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.