Java Integer toUnsignedString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, Integer.toUnsignedString(n) converts the 32 bits of n into text as if the number were unsigned, so the result is never negative. Values from 0 to 2147483647 print as usual, and negative values print as the large numbers that share the same bits.

A second argument chooses the base. Use it when you store IDs, checksums, or addresses in an int that really represent unsigned values.

A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.toUnsignedString(...). Integer lives in java.lang, so no import is needed. toUnsignedString() was added in Java 8.

Syntax

Pass the number, and optionally the radix.

Java
String a = Integer.toUnsignedString(-1);        // "4294967295"
String b = Integer.toUnsignedString(100);       // "100"
String c = Integer.toUnsignedString(-1, 16);    // "ffffffff"
CallWhat it does
Integer.toUnsignedString(n)Unsigned decimal text of the 32 bits of n.
Integer.toUnsignedString(n, radix)Unsigned text in the given base, from 2 to 36.
Integer.toUnsignedLong(n)The same value as a positive long number.
Integer.toString(n)Signed text: -1 stays "-1".

The method does not change the stored bits; it only chooses how to print them. A radix outside 2 to 36 falls back to 10.

Library declarations
Java
// java.lang.Integer
public static String toUnsignedString(int i)
public static String toUnsignedString(int i, int radix)

Parameters

ParameterDescription
i (int)The 32 bits to show. They are read as an unsigned number.
radix (int)The base, from 2 to 36. Optional; the default is 10.

Return Value

Return ValueDescription
StringThe unsigned text, from "0" up to "4294967295" in decimal. It never starts with a minus sign.

Compare with Similar Methods

Compare with the signed and bit-pattern versions:

MethodHow it differs
toString(n)Signed text; negative numbers keep their minus sign.
toHexString(n)Always unsigned, but only in base 16.
toUnsignedLong(n)Gives the unsigned value as a long number instead of text.
parseUnsignedInt(s)The reverse: reads unsigned text back into an int.

Java Examples

Example 1 — Print Negative Numbers as Unsigned

Print several values with the normal toString and with toUnsignedString side by side.

Java
public class IntegerToUnsignedStringDemo {
    public static void main(String[] args) {
        int[] numbers = {100, Integer.MAX_VALUE, Integer.MIN_VALUE, -2, -1};
        for (int n : numbers) {
            System.out.println(Integer.toString(n) + " -> " + Integer.toUnsignedString(n));
        }
    }
}

How It Works

  1. Non-negative values match. 100 and MAX_VALUE print the same in both forms, because their top bit is 0.
  2. MIN_VALUE. It is just the top bit, which is 231, or 2147483648, when read as unsigned.
  3. -1 is the largest. All 32 bits are ones, so the unsigned value is 232 minus 1, which is 4294967295.
  4. -2 is one less. Flipping the lowest bit gives 4294967294.

Example 2 — Print the Same Bits in Several Bases

Pass a radix to print an all-ones value in different bases, and compare with toString.

Java
public class IntegerToUnsignedStringRadix {
    public static void main(String[] args) {
        int allOnes = -1;

        System.out.println("base 10: " + Integer.toUnsignedString(allOnes));
        System.out.println("base 16: " + Integer.toUnsignedString(allOnes, 16));
        System.out.println("base 8:  " + Integer.toUnsignedString(allOnes, 8));
        System.out.println("base 36: " + Integer.toUnsignedString(allOnes, 36));
        System.out.println("base 2:  " + Integer.toUnsignedString(allOnes, 2).length() + " digits");
        System.out.println("signed:  " + Integer.toString(allOnes, 16));
    }
}

How It Works

  1. One value, many spellings. The same 32 bits are 4294967295 in decimal and ffffffff in hex.
  2. Octal and base 36. The octal text is 37777777777, and base 36 needs only seven characters.
  3. Binary length. The base 2 text is 32 ones, so the example prints its length instead of the long text.
  4. The signed form. toString(allOnes, 16) prints -1, which hides the bit pattern.

Example 3 — Store an IPv4 Address in an int

Pack the four parts of an IP address into one int, then print it as a plain number and in dotted form.

Java
public class IntegerToUnsignedStringIp {
    public static void main(String[] args) {
        int ip = (192 << 24) | (168 << 16) | (1 << 8) | 10;

        System.out.println("as signed int:   " + ip);
        System.out.println("as unsigned:     " + Integer.toUnsignedString(ip));

        String dotted = ((ip >>> 24) & 0xFF) + "." + ((ip >>> 16) & 0xFF) + "."
                + ((ip >>> 8) & 0xFF) + "." + (ip & 0xFF);
        System.out.println("dotted form:     " + dotted);
    }
}

How It Works

  1. Four bytes in one int. Each part is shifted into its own 8-bit slot: 192 on top, then 168, 1, and 10.
  2. The top bit is set. 192 is above 127, so the stored int is negative: -1062731510.
  3. Unsigned shows the real number. toUnsignedString gives 3232235786, the number you would see in a network tool.
  4. Back to dotted text. >>> shifts without copying the sign bit, and & 0xFF keeps one byte at a time.

Frequently Asked Questions

It prints the 32 bits of an int as an unsigned number. Integer.toUnsignedString(-1) returns "4294967295".
They match for 0 up to Integer.MAX_VALUE. For negative ints, toString keeps the minus sign, while toUnsignedString shows the large non-negative number with the same bits.
Java 8. Earlier versions need Long and a mask, for example Long.toString(n & 0xFFFFFFFFL).
No. It only changes how the bits are shown. The int variable keeps its value.
Call Integer.parseUnsignedInt(text). The plain parseInt rejects anything above 2147483647.

Next: valueOf()

Get an Integer object from an int or a String.

valueOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful