C++ Implementation
Standard Library Conversion Methods
1. Using Standard Conversion Functions
#include <iostream>
#include <string>
#include <bitset>
class NumberConverter {
public:
// Decimal to Binary
static std::string decimalToBinary(int decimal) {
return std::bitset<32>(decimal).to_string();
}
// Binary to Decimal
static int binaryToDecimal(const std::string& binary) {
return std::stoi(binary, nullptr, 2);
}
// Hexadecimal Conversions
static int hexToDecimal(const std::string& hex) {
return std::stoi(hex, nullptr, 16);
}
static std::string decimalToHex(int decimal) {
char buffer[20];
sprintf(buffer, "%X", decimal);
return std::string(buffer);
}
};
Custom Conversion Class
Comprehensive Number System Converter
class AdvancedNumberConverter {
private:
// Utility method for digit to value conversion
static int charToValue(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
throw std::invalid_argument("Invalid digit");
}
public:
// Generic base conversion method
static int toDecimal(const std::string& number, int base) {
int decimal = 0;
int power = 0;
for (int i = number.length() - 1; i >= 0; --i) {
decimal += charToValue(number[i]) * std::pow(base, power++);
}
return decimal;
}
// Decimal to any base conversion
static std::string fromDecimal(int decimal, int base) {
if (decimal == 0) return "0";
const std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string result;
while (decimal > 0) {
result = digits[decimal % base] + result;
decimal /= base;
}
return result;
}
};
Practical Implementation Example
int main() {
// Conversion demonstrations
try {
// Standard conversions
std::cout << "Decimal to Binary: "
<< NumberConverter::decimalToBinary(42) << std::endl;
// Advanced conversions
std::cout << "Binary to Decimal: "
<< AdvancedNumberConverter::toDecimal("101010", 2) << std::endl;
// Hex conversions
std::cout << "Hex to Decimal: "
<< AdvancedNumberConverter::toDecimal("2A", 16) << std::endl;
// Decimal to different bases
std::cout << "Decimal 42 in Base 3: "
<< AdvancedNumberConverter::fromDecimal(42, 3) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Conversion error: " << e.what() << std::endl;
}
return 0;
}
Conversion Method Complexity
Conversion Type |
Time Complexity |
Space Complexity |
Decimal to Base |
O(log n) |
O(log n) |
Base to Decimal |
O(k) |
O(1) |
Error Handling Strategies
graph TD
A[Input Validation] --> B{Valid Input?}
B -->|Yes| C[Perform Conversion]
B -->|No| D[Throw Exception]
C --> E[Return Converted Value]
D --> F[Handle Error Gracefully]
Best Practices
- Use built-in type conversion methods when possible
- Implement robust error handling
- Consider performance for large number conversions
- Validate input before conversion
Compilation and Execution
To compile on Ubuntu 22.04:
g++ -std=c++11 number_converter.cpp -o number_converter
./number_converter
Note: LabEx recommends practicing these implementation techniques to master number system conversions in C++.