Integer.toUnsignedString() method in Java

In this post, we are going to discuss the toUnsignedString() method of the Integer wrapper class in detail. It is a static method and has two overloaded implementations –

Let’s have a look at the above methods one by one.

public static String toUnsignedString(int i, int radix)

  • What does it do? – It will convert the integer passed as the first argument into the string by using the second argument as a base or radix. Here, the integer would be treated as an unsigned integer
  • What does it return? It will return a string representation of the integer passed in the argument by treating it as the unsigned integer

Didn’t understand things yet? Don’t worry, we’ve got you covered.

Now, what are signed and unsigned integers? Signed integers contain both positive and negative numbers and their range is from [-2147483648 to 2147483647] whereas unsigned integers contain only the non-negative numbers ranging from [0 to 4294967295].

There are a few points to remember while using toUnsignedString(int i, int radix)

  • We cannot use the long data type with toUnsignedString() method of the Integer class. We can only use the integer data type here
  • Radix passed in the second argument must be between 2 and 36 ( both inclusive). If radix < 2 or radix > 36, then radix = 10 would be used instead
  • Positive integers are treated as it is but as we working with unsigned values, so, negative integer arguments would be converted to unsigned integers by adding 232(4294967296) to them. Keeping that in mind, -100 would now be converted to an unsigned integer by adding 232 to it, which means now we would be converting (-100+232 = 4294967196 ) to the string with the given base
Converting positive integer to string by using toUnsignedString(int i, int radix) method
public class Codekru {

	public static void main(String[] args) {

		int i = 100;
		int radix = 16;

		String str = Integer.toUnsignedString(i, radix);
		System.out.println("String representation of i = " + i + " in radix " + radix + " is : " + str);

	}

}

Output –

String representation of i = 100 in radix 16 is : 64

Converting negative integer to string by using toUnsignedString(int i, int radix) method
public class Codekru {

	public static void main(String[] args) {

		// using negative integer
		int i = -100;
		int radix = 16;

		String str = Integer.toUnsignedString(i, radix);
		System.out.println("String representation of i = " + i + " in radix " + radix + " is : " + str);

	}

}

Output –

String representation of i = -100 in radix 16 is : ffffff9c

So, here -100 was converted to an unsigned integer by adding 232(4294967296) to it, and then the resulting number ( 4294967196 ) is converted to the string.

public static String toUnsignedString(int i)

  • What does it do? – It will convert the integer passed as the first argument into the string by treating the argument as an unsigned integer. So, here the number 100 would be converted to string “100”
  • What does it return? It will return a string representation of the integer passed in the argument by treating it as the unsigned integer

Here also, the negative integer will be converted to the unsigned integer by adding 232(4294967296) to it.

Code Example
public class Codekru {

	public static void main(String[] args) {

		int positiventeger = 100;
		int negativeenteger = -100;

		String str1 = Integer.toUnsignedString(positiventeger);
		String str2 = Integer.toUnsignedString(negativeenteger);

		System.out.println("String representation of i = " + positiventeger + " is : " + str1);
		System.out.println("String representation of i = " + negativeenteger + " is : " + str2);

	}

}

Output –

String representation of i = 100 is : 100
String representation of i = -100 is : 4294967196
Internal implementation of toUnsignedString(int i) method

toUnsignedString(int i) uses toUnsignedLong() and Long.toString() methods to convert an integer into the corresponding string.

    public static String toUnsignedString(int i) {
        return Long.toString(toUnsignedLong(i));
    }
Q- What if we pass radix > 36 in the arguments?

If radix < 2 or radix > 36, then 10 would be used as the radix in place of the radix passed in the arguments.

public class Codekru {

	public static void main(String[] args) {

		int i = 100;
		int radix = 40; // radix > 36

		String str = Integer.toUnsignedString(i, radix);
		System.out.println("String representation of i = " + i + " in radix " + radix + " is : " + str);

	}

}

Output –

String representation of i = 100 in radix 37 is : 100

Please visit this link if you want to know more about the Integer wrapper class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *