Skip to content
Tutorials Center
My learning

Topics

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.

Java
Integer.toString(255);          // "255"
Integer.toBinaryString(10);     // "1010"
Integer.toHexString(255);       // "ff"

Try it

Java All the conversions side by side

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.

Java Padding, grouping and round trips

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

Java
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 with 1. To show a signed binary number use toString(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 use String.format("%X", n).
  • Which conversion to use for concatenation. "" + n, String.valueOf(n) and Integer.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 a to z, lowercase.
  • HexFormat (Java 17). HexFormat.of().toHexDigits(n) always yields 8 digits, and also handles byte arrays.
  • Integer.MIN_VALUE converts fine; only negating it first goes wrong.
  • Localization. None of these methods use a locale. For thousands separators or localized digits use NumberFormat or %,d.
  • Round trip. Integer.parseInt(Integer.toString(i, r), r) always returns i, but parseInt(toHexString(-1), 16) throws, because ffffffff is too big for a signed int. Use parseUnsignedInt(..., 16) instead.