In Java, Integer.decode(text) turns text into an Integer and reads the base from a prefix, the same way Java source code does. 0x1F and #1F are hexadecimal, a leading 0 means octal, and everything else is decimal.
An optional - or + may come first. Unlike parseInt, decode lets users type the base themselves, which is handy for configuration values and colours.
A static method of java.lang.Integer (since Java 1.2): call it on the class, as Integer.decode(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the text, with an optional sign and prefix.
Java
Integer a = Integer.decode("0x1F"); // 31 (hex)
Integer b = Integer.decode("#FF"); // 255 (hex)
Integer c = Integer.decode("010"); // 8 (octal!)
Integer d = Integer.decode("-42"); // -42 (decimal)
Call
What it does
Integer.decode("0x1F")
Hexadecimal: 0x or 0X prefix.
Integer.decode("#FF")
Hexadecimal: # prefix, common in colours.
Integer.decode("010")
Octal: a leading zero before more digits.
Integer.decode("-0x10")
The sign comes before the prefix.
A leading zero means octal, so decode("010") is 8 and decode("08") fails. A plain "0" is just zero.
Library declarations
Java
// java.lang.Integer
public static Integer decode(String nm) throws NumberFormatException
Parameters
Parameter
Description
nm (String)
The text to read. It may begin with - or +, then an optional prefix, then digits.
Return Value
Return Value
Description
Integer
The Integer object for the number. Unbox it with intValue() or let Java unbox it automatically.