Introduction
In Java programming, understanding how to parse and convert numbers between different radix systems is a crucial skill for developers. This tutorial explores the techniques and methods for working with various number bases, providing insights into Java's powerful number parsing capabilities and demonstrating practical approaches to handling different number representations.
Radix Fundamentals
What is Radix?
Radix, also known as base or number system, represents the number of unique digits used to represent numbers. In programming, radix defines how numbers are interpreted and converted between different number systems.
Common Number Systems
| Radix | Name | Digits | Prefix | Example |
|---|---|---|---|---|
| 2 | Binary | 0-1 | 0b | 1010 |
| 8 | Octal | 0-7 | 0 | 755 |
| 10 | Decimal | 0-9 | None | 255 |
| 16 | Hexadecimal | 0-9, A-F | 0x | FF |
Radix in Java
Java provides multiple methods to parse and convert numbers with different radixes:
graph LR
A[Integer.parseInt()] --> B[Supports multiple radix]
A --> C[Converts string to integer]
B --> D[Radix range: 2-36]
Code Example: Number Parsing
public class RadixDemo {
public static void main(String[] args) {
// Binary to Decimal
int binary = Integer.parseInt("1010", 2); // Result: 10
// Hexadecimal to Decimal
int hex = Integer.parseInt("FF", 16); // Result: 255
// Octal to Decimal
int octal = Integer.parseInt("755", 8); // Result: 493
System.out.println("Conversions: " + binary + ", " + hex + ", " + octal);
}
}
Key Considerations
- Java supports radix from 2 to 36
- Useful for parsing different number representations
- Helps in data conversion and encoding scenarios
Learn more about number parsing with LabEx's comprehensive Java programming tutorials.
Number Parsing Methods
Java Parsing Methods Overview
Java provides multiple methods for parsing numbers with different radixes:
graph LR
A[Parsing Methods] --> B[Integer.parseInt()]
A --> C[Integer.valueOf()]
A --> D[Long.parseLong()]
A --> E[Byte.parseByte()]
Key Parsing Methods
| Method | Return Type | Radix Range | Use Case |
|---|---|---|---|
| Integer.parseInt() | int | 2-36 | Basic integer parsing |
| Integer.valueOf() | Integer object | 2-36 | Object-based parsing |
| Long.parseLong() | long | 2-36 | Large number parsing |
| Byte.parseByte() | byte | 2-36 | Small number parsing |
Detailed Method Examples
1. Integer.parseInt() Method
public class ParsingDemo {
public static void main(String[] args) {
// Binary to Decimal
int binaryValue = Integer.parseInt("1010", 2); // Result: 10
// Hexadecimal to Decimal
int hexValue = Integer.parseInt("FF", 16); // Result: 255
// Custom radix parsing
int customRadix = Integer.parseInt("Z", 36); // Result: 35
System.out.println("Parsed values: " +
binaryValue + ", " + hexValue + ", " + customRadix);
}
}
2. Integer.valueOf() Method
public class ValueOfDemo {
public static void main(String[] args) {
// Object-based parsing
Integer decimalObj = Integer.valueOf("100"); // Decimal
Integer binaryObj = Integer.valueOf("1010", 2); // Binary
System.out.println("Parsed objects: " +
decimalObj + ", " + binaryObj);
}
}
Error Handling
public class ErrorHandlingDemo {
public static void main(String[] args) {
try {
// Incorrect radix or format will throw NumberFormatException
int invalidParse = Integer.parseInt("ABC", 10);
} catch (NumberFormatException e) {
System.out.println("Parsing error: " + e.getMessage());
}
}
}
Best Practices
- Always use try-catch for robust parsing
- Verify input before parsing
- Choose appropriate method based on number type
- Consider radix limitations (2-36)
Explore more advanced parsing techniques with LabEx's Java programming resources.
Practical Conversion Examples
Conversion Scenarios
graph LR
A[Conversion Types] --> B[Decimal to Other Bases]
A --> C[Other Bases to Decimal]
A --> D[Between Different Bases]
Comprehensive Conversion Techniques
1. Decimal to Other Bases
public class DecimalConversionDemo {
public static void main(String[] args) {
int decimalNumber = 255;
// Decimal to Binary
String binary = Integer.toBinaryString(decimalNumber);
// Decimal to Hexadecimal
String hexadecimal = Integer.toHexString(decimalNumber);
// Decimal to Octal
String octal = Integer.toOctalString(decimalNumber);
System.out.println("Conversions:");
System.out.println("Binary: " + binary);
System.out.println("Hexadecimal: " + hexadecimal);
System.out.println("Octal: " + octal);
}
}
2. Custom Base Conversion
public class CustomBaseConversion {
public static void main(String[] args) {
// Convert between arbitrary bases
String result = Integer.toString(255, 36); // Base 36
System.out.println("Base 36 representation: " + result);
// Parse custom base
int parsed = Integer.parseInt("FF", 16);
System.out.println("Parsed hexadecimal: " + parsed);
}
}
Conversion Methods Comparison
| Method | Input | Output | Radix Range |
|---|---|---|---|
| Integer.toBinaryString() | Decimal | Binary String | 2 |
| Integer.toHexString() | Decimal | Hexadecimal String | 16 |
| Integer.toOctalString() | Decimal | Octal String | 8 |
| Integer.toString(num, radix) | Decimal, Base | Custom Base String | 2-36 |
3. Advanced Conversion Handling
public class AdvancedConversionDemo {
public static void main(String[] args) {
try {
// Complex conversion with error handling
String input = "1010"; // Binary representation
int base = 2;
int convertedValue = Integer.parseInt(input, base);
// Multiple base conversions
String[] bases = {"Binary", "Octal", "Hexadecimal"};
int[] radixes = {2, 8, 16};
for (int i = 0; i < bases.length; i++) {
String converted = Integer.toString(convertedValue, radixes[i]);
System.out.printf("%s: %s%n", bases[i], converted);
}
} catch (NumberFormatException e) {
System.out.println("Conversion error: " + e.getMessage());
}
}
}
Practical Considerations
- Always validate input before conversion
- Use appropriate error handling
- Understand radix limitations
- Choose the right conversion method
Enhance your Java programming skills with LabEx's comprehensive tutorials and practical examples.
Summary
By mastering radix parsing in Java, developers can efficiently handle number conversions across different number systems. The techniques and methods discussed in this tutorial provide a comprehensive understanding of how to parse and transform numbers, enhancing programming flexibility and expanding numerical manipulation skills in Java applications.



