java.lang.Long parseUnsignedLong(String s, int radix)

Description

The Long.parseUnsignedLong(String s, int radix) java method

Parses the string argument as an unsigned long 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 long, 2^64-1.

Make a note that the parseUnsignedLong() method of Long class is static thus it should be accessed statically which means the we would be calling this method in this format:

Long.parseUnsignedLong(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseUnsignedLong method non statically.

Method Syntax

public static int parseUnsignedLong(String s, int radix)

Method Argument

Data Type Parameter Description
String s the String containing the unsigned integer representation to be parsed
int radix the radix to be used while parsing s.

Method Returns

The parseUnsignedLong(String s, int radix) method of Long class returns the unsigned long represented by the string argument in the specified radix.

Compatibility

Requires Java 1.8 and up

Java Long parseUnsignedLong(String s, int radix) Example

Below is a simple java example on the usage of parseUnsignedLong(String s, int radix) method of Long class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * parseUnsignedLong(String s, int radix) method of Long class
 */

public class LongParseUnsignedLongRadixExample {

	public static void main(String[] args) {
		
		// Ask for user input
		System.out.print("Enter a value:");
		
		// declare a scanner object to read the user input
		Scanner s = new Scanner(System.in);
		
		// assign the input to a variable
		String value = s.nextLine();
		
		// ask for radix
		System.out.print("Radix:");
		
		// assign the radix input to a variable
		int radix = s.nextInt();
		
		
		// get the parseUnsignedLong() method result using radix input
		Long result = Long.parseUnsignedLong(value, radix);
		
		// print the result
		System.out.println("Result:"+result);
		
		// close the scanner object
		s.close();
		
		/*
		 * This method will yield the same result as that of the parseUnsignedLong(value)
		 * if the radix is 10
		 */
		
		
	}

}

Sample Output

Below is the sample output when you run the above example.

Java Long parseUnsignedLong(String s, int radix) example output