Ad space (banner)
Java Lessons
Lesson 62 / 68

Converting Number Bases (Binary, Hex)

This lesson covers converting number bases in Java — binary, hex, and more — so you understand how a computer represents numbers internally. It's for anyone searching "Java number base conversion usage."

Integer.toBinaryString() and Integer.toHexString() convert a number into a binary or hexadecimal string. Going the other way, Integer.parseInt(string, radix) converts a string in that base back into a decimal number.

The example converts 42 into a binary string with Integer.toBinaryString(42), and into a hex string with Integer.toHexString(42). Having a dedicated method for each purpose is characteristic of Java.

A common early mistake is the relationship between a base's radix and its actual values. Hexadecimal uses the letters a through f after 0 through 9 — a notation rule that takes some getting used to compared to decimal intuition.

Binary is commonly used inside a computer, and hexadecimal is commonly used for things like color codes — worthwhile foundational knowledge to remember.

📖 Reference code
✍️ Your code
Type your code, then press "Run"

🧪 This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser — nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)