Practical Coding Examples
Real-World Radix Conversion Scenarios
Network Address Conversion
public class NetworkAddressConverter {
public static void main(String[] args) {
// Convert IP address between decimal and binary
String ipAddress = "192.168.1.1";
long decimalIp = ipToDecimal(ipAddress);
String binaryIp = Long.toBinaryString(decimalIp);
System.out.println("Decimal IP: " + decimalIp);
System.out.println("Binary IP: " + binaryIp);
}
public static long ipToDecimal(String ipAddress) {
String[] octets = ipAddress.split("\\.");
long result = 0;
for (String octet : octets) {
result = result * 256 + Integer.parseInt(octet);
}
return result;
}
}
Cryptographic Key Generation
public class CryptoKeyGenerator {
public static void main(String[] args) {
// Generate cryptographic key using different radix
int keyLength = 16;
String hexKey = generateRandomKey(keyLength, 16);
String binaryKey = generateRandomKey(keyLength, 2);
System.out.println("Hex Key: " + hexKey);
System.out.println("Binary Key: " + binaryKey);
}
public static String generateRandomKey(int length, int radix) {
StringBuilder key = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int digit = random.nextInt(radix);
key.append(Integer.toString(digit, radix).toUpperCase());
}
return key.toString();
}
}
Conversion Workflow Patterns
graph TD
A[Input Data] --> B{Determine Source Radix}
B --> |Known Radix| C[Parse Input]
B --> |Unknown Radix| D[Detect Radix]
C --> E[Convert to Decimal]
D --> E
E --> F{Target Radix}
F --> G[Convert to Target Radix]
G --> H[Output Converted Value]
Complex Conversion Utility
public class RadixUtility {
public static String convertBetweenRadix(String input, int sourceRadix, int targetRadix) {
// Universal radix conversion method
try {
// Convert to decimal first
long decimalValue = Long.parseLong(input, sourceRadix);
// Convert from decimal to target radix
return Long.toString(decimalValue, targetRadix).toUpperCase();
} catch (NumberFormatException e) {
return "Conversion Error: Invalid Input";
}
}
public static void main(String[] args) {
// Example conversions
String[] conversions = {
convertBetweenRadix("1010", 2, 10), // Binary to Decimal
convertBetweenRadix("255", 10, 16), // Decimal to Hex
convertBetweenRadix("FF", 16, 2) // Hex to Binary
};
for (String result : conversions) {
System.out.println(result);
}
}
}
Conversion Type |
Method |
Time Complexity |
Memory Usage |
parseInt |
O(n) |
Low |
Minimal |
BigInteger |
O(n^2) |
High |
Moderate |
Custom Method |
O(n) |
Varies |
Depends |
Advanced Conversion Techniques
- Dynamic Radix Detection
- Error-Tolerant Conversion
- Optimized Large Number Handling
LabEx Recommended Practices
- Use built-in Java conversion methods
- Implement robust error handling
- Consider performance implications
- Validate input before conversion
By mastering these practical coding examples, developers can effectively handle complex radix conversion scenarios in real-world applications, enhancing their programming skills in the LabEx ecosystem.