Signup/Sign In

Java Long toUnsignedString() Method

Java toUnsignedString() method is a part of the Long class of the java.lang package. This method returns the String object of the long value passed as an argument as an unsigned decimal value.

In short, this method is used to convert long value into an unsigned String.

Syntax:

public static String toUnsignedString(long i)  

Parameter:

The parameter passed is the long value whose String representation of the unsigned decimal value is to be returned.

Returns:

Returns the unsigned base 10 equivalent of the long value passed as a parameter as a String.

Example 1:

Here, the long values are converted into its equivalent unsigned decimal values and represented as Strings.

import java.lang.Long;

public class StudyTonight
{  
    public static void main(String[] args)
      {  
        long a = 40L;
        long b = -56L;
        System.out.println("Equivalent String is : "+Long.toUnsignedString(a));  //returns the unsigned decimal value as a String
        System.out.println("Equivalent String is : "+Long.toUnsignedString(b));  
      }  
} 


Equivalent String is : 40
Equivalent String is : 18446744073709551560

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.print("Enter the value ");  
			Scanner sc = new Scanner(System.in);  
			long val = sc.nextLong();  
			String s = Long.toUnsignedString(val); //converting to unsigned decimal value as a String
			System.out.println("String value is : "+ s);  
		}
		catch(Exception e)
		{
			System.out.println("Invalid input!!");
		}
	}  
}  


Enter the value 94
String value is : 94
***********************
Enter the value -52
String value is : 18446744073709551564
************************
Enter the value 0x799
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.