Integer decode Radix Prefix Parsing
Integer.decode reads 0x and # prefixes as hex and a leading 0 as octal, while parseInt accepts an explicit radix from 2 to 36 for everything else.
Code
public class RadixParsing {
public static void main(String[] args) {
// decode() 按 prefix 识别进制:0x / 0X / # 是 hex,0 开头是 octal
System.out.println("decode 0xff: " + Integer.decode("0xff"));
System.out.println("decode #10: " + Integer.decode("#10"));
System.out.println("decode 017: " + Integer.decode("017"));
// parseInt 显式指定 radix(2-36 进制),不认任何前缀
System.out.println("parseInt 1010 radix 2: " + Integer.parseInt("1010", 2));
System.out.println("parseInt zz radix 36: " + Integer.parseInt("zz", 36));
// toString 对称地按进制输出
System.out.println("toString 255 radix 16: " + Integer.toString(255, 16));
System.out.println("toString 8 radix 2: " + Integer.toString(8, 2));
// 0 开头的 octal 串里混进 8/9 会抛 NumberFormatException:"09" 不是合法八进制
try {
Integer.decode("09");
} catch (NumberFormatException bad) {
System.out.println("bad octal: " + bad.getClass().getSimpleName());
}
}
}How the snippet works
All of the logic lives in the class RadixParsing and the function main.
Caveats
- Nothing to install: the snippet stays inside the standard library.
Applying it
The main signature reads public static void main(String[] args)
The file announces its steps in the output: decode 0xff:, decode #10:, decode 017:.
Statements that matter
Integer.decode("09");
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does Integer decode Radix Prefix Parsing print when run?
- No setup needed: run the file and look for labeled lines like `decode 0xff:`, `decode #10:`, `decode 017:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.