Java Reference ยท java.lang
Java Integer.toString() and toBinaryString()
Reference for converting Java ints to text: Integer.toString, toBinaryString, toHexString, toOctalString, radixes, padding and negative numbers.
Integer offers several methods that turn an int into text. Integer.toString(int) produces the usual decimal form. toBinaryString, toHexString and toOctalString print the raw 32 bits as an unsigned number in base 2, 16 or 8. Integer.toString(int, radix) handles any base from 2 to 36 and keeps the sign.
The difference between "signed" and "unsigned" output matters for negative numbers: Integer.toString(-1, 2) is -1, while Integer.toBinaryString(-1) is thirty-two ones.
Integer.toString(255); // "255" Integer.toBinaryString(10); // "1010" Integer.toHexString(255); // "ff"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static String toString(int i) static String toString(int i, int radix) static String toBinaryString(int i) static String toOctalString(int i) static String toHexString(int i) static String toUnsignedString(int i) static String toUnsignedString(int i, int radix) String toString() // instance method
| Name | Description |
|---|---|
i |
The value to convert. |
radix |
Base from 2 to 36. An invalid radix silently falls back to 10 for toString(int, int). |
Returns: the text, never null. Hexadecimal digits are lowercase. There is no prefix such as 0x and no leading zeros.
Throws: nothing. (The instance toString() is called on a non-null object, otherwise you get a NullPointerException from the call itself.)
Usage notes
- Binary, octal and hex are unsigned. Negative inputs show the two's-complement bits:
toBinaryString(-8)has 32 digits and begins with1. To show a signed binary number usetoString(i, 2). - No padding. The result has no leading zeros. Pad with
String.format("%8s", ...),repeat, or"%08X"for hex. - Uppercase hex. Call
.toUpperCase(), or useString.format("%X", n). - Which conversion to use for concatenation.
"" + n,String.valueOf(n)andInteger.toString(n)give the same result. The last two make intent explicit; none is meaningfully faster in modern JVMs. - Digits above 9. In radix 11 to 36 digits are the letters
atoz, lowercase. - HexFormat (Java 17).
HexFormat.of().toHexDigits(n)always yields 8 digits, and also handles byte arrays. Integer.MIN_VALUEconverts fine; only negating it first goes wrong.- Localization. None of these methods use a locale. For thousands separators or localized digits use
NumberFormator%,d. - Round trip.
Integer.parseInt(Integer.toString(i, r), r)always returnsi, butparseInt(toHexString(-1), 16)throws, becauseffffffffis too big for a signedint. UseparseUnsignedInt(..., 16)instead.