Signup/Sign In

Java Long toUnsignedString(long i, int radix) Method

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

It must be noted that if the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.Also since the first argument is treated as an unsigned value, no leading sign character is printed.

If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character.

Syntax:

public static String toUnsignedString(long i, int radix)  

Parameters:

The parameters passed are long i whose unsigned integer value is returned as a String and int radix which defines the base for the String conversion.

Returns:

Returns the unsigned String representation of the long value passed in accordance with the radix value passed as the parameter.

Example 1:

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

import java.lang.Long;

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		long a = -78L;
		int b = 78;
		int d = 10;
		int h = 16;
		int o = 8;

		String s1 = Long.toUnsignedString(a,d); //Returns the unsigned string representation of the long value with radix 10  
		System.out.println("Equivalent String Value = " + s1);   

		String s2 = Long.toUnsignedString(a,h);  //Returns the unsigned string representation of the long value with radix 16  
		System.out.println("Equivalent String Value = " + s2);  

		String s3 = Long.toUnsignedString(a, o); //Returns the unsigned string representation of the long value with radix 8   
		System.out.println("Equivalent String Value = " + s3);  

		String s4 = Long.toUnsignedString(b, d);  //Returns the unsigned string representation of the long value with radix 10  
		System.out.println("Equivalent String Value = " + s4);  

		String s5 = Long.toUnsignedString(b, h);  //Returns the unsigned string representation of the long value with radix 16  
		System.out.println("Equivalent String Value = " + s5);  

		String s6 = Long.toUnsignedString(b, o); //Returns the unsigned string representation of the long value with radix 8   
		System.out.println("Equivalent String Value = " + s6);  
	}  
}


Equivalent String Value = 18446744073709551538
Equivalent String Value = ffffffffffffffb2
Equivalent String Value = 1777777777777777777662
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);  
			long val = sc.nextLong();  
			int b = sc.nextInt();  
			System.out.println("Output: "+Long.toUnsignedString(val, b));  //returns unsigned string with equivalent base
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
} 


Enter the value and base
7445 8
Output: 16425
*********************************
Enter the value and base
-688 16
Output: fffffffffffffd50
*********************************
Enter the value and base
0x67 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.