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"
Call
What 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
Parameter
Description
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 Value
Description
String
The unsigned text, from "0" up to "4294967295" in decimal. It never starts with a minus sign.
as signed int: -1062731510
as unsigned: 3232235786
dotted form: 192.168.1.10
How It Works
Four bytes in one int. Each part is shifted into its own 8-bit slot: 192 on top, then 168, 1, and 10.
The top bit is set. 192 is above 127, so the stored int is negative: -1062731510.
Unsigned shows the real number.toUnsignedString gives 3232235786, the number you would see in a network tool.
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.