In Java, converting a hexadecimal (hex) number to a decimal is a common task, especially in programming scenarios involving computer memory, networking, and color codes. Hexadecimal numbers use base 16, while decimal numbers use base 10.
In this blog, we will explore different ways to convert hex to decimal in Java using built-in methods and manual conversion techniques.
Understanding Hexadecimal and Decimal Number Systems
- Hexadecimal (Base 16): Uses 0-9 and A-F (where A=10, B=11, …, F=15).
- Decimal (Base 10): Uses 0-9.
Example Conversion:
Hex “1A” = 1 × 16¹ + 10 × 16⁰ = 26 (Decimal)
Convert Hex to Decimal in Java
Method 1: Using Integer.parseInt()
The simplest way to convert a hexadecimal string to a decimal number is by using Java’s built-in method Integer.parseInt()
.
public class HexToDecimalExample1 {
public static void main(String[] args) {
String hexNumber = "1A"; // Hexadecimal value
int decimalNumber = Integer.parseInt(hexNumber, 16); // Convert to decimal
System.out.println("Decimal Equivalent: " + decimalNumber);
}
}
Output:
Decimal Equivalent: 26
🔹 The method Integer.parseInt(hexNumber, 16)
converts the hex string to a decimal integer.
Method 2: Using BigInteger
for Large Numbers
For large hexadecimal numbers, use BigInteger, as Integer.parseInt()
cannot handle values beyond Integer.MAX_VALUE
.
import java.math.BigInteger;
public class HexToDecimalBigInteger {
public static void main(String[] args) {
String hexNumber = "1A3F56"; // Large hexadecimal value
BigInteger decimalNumber = new BigInteger(hexNumber, 16); // Convert to decimal
System.out.println("Decimal Equivalent: " + decimalNumber);
}
}
Output:
Decimal Equivalent: 1717174
🔹 BigInteger
allows handling very large hexadecimal values without overflow issues.
Method 3: Manual Hex to Decimal Conversion in Java
To manually convert hex to decimal, follow these steps:
- Start from the rightmost digit.
- Multiply each digit by 16 raised to its position index.
- Sum all results.
🔹 Example: Convert “1A” to Decimal manually
1 × (16^1) + A (10) × (16^0)
= 16 + 10
= 26 (Decimal)
🔹 Java Code for Manual Conversion:
public class HexToDecimalManual {
public static void main(String[] args) {
String hex = "1A";
int decimal = 0;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
int digit = Character.digit(hexChar, 16); // Convert hex char to decimal
decimal = decimal * 16 + digit;
}
System.out.println("Decimal Equivalent: " + decimal);
}
}
Output:
Decimal Equivalent: 26
🔹 This approach iterates over each character in the hex string and converts it manually.
Comparing Different Approaches
Method | Best For | Handles Large Numbers? | Simplicity |
---|---|---|---|
Integer.parseInt() |
Small hex numbers | ❌ No | ✅ Easy |
BigInteger |
Large hex numbers | ✅ Yes | ✅ Easy |
Manual Conversion | Learning and custom implementations | ✅ Yes | ❌ More complex |
Conclusion
Converting hexadecimal to decimal in Java is easy with built-in methods like Integer.parseInt()
and BigInteger
, but you can also use manual conversion for learning purposes.
🚀 Start experimenting with hex-to-decimal conversions in Java today!