Skip to content

Commit

Permalink
added a number base converter
Browse files Browse the repository at this point in the history
  • Loading branch information
bbsv58 committed Feb 18, 2025
1 parent 0aa2282 commit 3d860f5
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Automations/Number_Base_Converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Number Base Converter

The Base Converter is a program written in C++ that allows users to convert numbers between different bases, ranging from base 2 to base 32.

## Features

- Converts numbers between bases 2 and 32.

- Supports conversion between decimal and other bases, including binary, octal, hexadecimal, etc.
- Simple interface for easy input and conversion.
- Remembers the last used bases for quick selection.

## Prerequisites

- A C++ compiler supporting C++17 or later (only if you want to compile it yourself).

## Installation

1. Clone the repository or download the source code files.

2. Navigate into the Number_Base_Converter folder.

3. Run "number_base_converter.exe" or compile it yourself by opening the commandline in the folder and follow the next step.

4. Compile the program using the following command (number_base_converter can be any name you want the Programm to have):

```bash
g++ -std=c++17 main.cpp -o number_base_converter

## Usage
1. Launch the program by running the executable.

2. Enter the number you wish to convert when prompted, followed by pressing Enter.

3. Enter the base of the number (between 2 and 32), followed by pressing Enter. The default base is 10.

4. Enter the target bases you want to convert the number to, followed by pressing Enter. You can choose from multiple bases like binary (2), octal (8), decimal (10), hexadecimal (16) (these are the default), or others between 2 and 32. Separate them using Spaces.

5. The program will display the converted number in the selected bases.

6. Optionally, you can convert another number by pressing enter and following the same process. If you want to quit the Programm press n and enter.

## Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.

## License

All the rights to this number base converter belong to [bbsv58](https://github.com/bbsv58).
158 changes: 158 additions & 0 deletions Automations/Number_Base_Converter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

// Function to validate if a number is valid for a given base
bool isValidNumber(const string &num, const int base) {
for (const char digit : num) {
int digitValue;
if (isdigit(digit)) {
digitValue = digit - '0';
} else if (isalpha(digit)){
digitValue = toupper(digit) - 'A' + 10;
} else {
return false;
}
if (digitValue >= base) {
return false;
}
}
return true;
}

// Function to convert a number from any base (2-32) to decimal
int64_t toDecimal(const string &num, const int base) {
int64_t value = 0;
int64_t power = 1;

for (int i = num.size() - 1; i >= 0; i--) {
const char digit = num[i];
int digitValue;

if (isdigit(digit)) {
digitValue = digit - '0';
} else {
digitValue = toupper(digit) - 'A' + 10;
}

value += digitValue * power;
power *= base;
}
return value;
}

// Function to convert a decimal number to any base (2-32)
string fromDecimal(int64_t decimal, const int base) {
if (decimal == 0) return "0";

string result;
while (decimal > 0) {
if (const int64_t remainder = decimal % base; remainder < 10) {
result += ('0' + remainder);
} else {
result += ('A' + (remainder - 10));
}
decimal /= base;
}

reverse(result.begin(), result.end());
return result;
}

// Function to handle base conversion & UI
void convertNumberBase() {
string number;
int fromBase = 10;
std::vector<int> targetBase = {2, 8, 10, 16};
string baseInput;
vector<int> lastUsedBases = targetBase; // Store the last used bases

do {
cout << "Enter the number: ";
cin >> number;

cout << "Enter the base of the number (2-32) [default: " << fromBase << "]: ";
cin.ignore();
string inputBase;
getline(cin, inputBase);

if (!inputBase.empty()) {
try {
if (const int newBase = stoi(inputBase); newBase >= 2 && newBase <= 32) {
fromBase = newBase;
} else {
cerr << "Error: Base must be between 2 and 32." << endl;
continue;
}
} catch ([[maybe_unused]]exception &e) {
cerr << "Error: Invalid base input." << endl;
continue;
}
}

if (!isValidNumber(number, fromBase)) {
cerr << "Error: The number contains invalid digits for base " << fromBase << "." << endl;
continue;
}

// Check if the user inputted bases or left it empty (defaults will be used)
targetBase.clear();
while (targetBase.empty()) {
cout << "Enter the bases to convert to (separated by spaces) [default: ";
for (int base : lastUsedBases) {
cout << base << " ";
}
cout << "]: ";
getline(cin, baseInput);

if (baseInput.empty()) {
targetBase = lastUsedBases; // Use last used bases if input is empty
break;
}

stringstream ss(baseInput);
string token;
while (ss >> token) {
try {
if (int base = stoi(token); base < 2 || base > 32) {
cerr << "Error: Base must be between 2 and 32." << endl;
} else {
targetBase.push_back(base);
}
} catch ([[maybe_unused]] exception &e) {
cerr << "Error: Invalid input for base." << endl;
}
}
}

try {
const int64_t decimal = toDecimal(number, fromBase);
for (const int base : targetBase) {
string converted = fromDecimal(decimal, base);
cout << "Converted number in base " << base << ": " << converted << endl;
}

lastUsedBases = targetBase;

} catch (const invalid_argument &e) {
cerr << "Error: " << e.what() << endl;
}

cout << "Do you want to convert another number? (y/n): ";
string input;
getline(cin, input);
if (!input.empty() && tolower(input[0]) == 'n') {
break;
}

} while (true);
}

int main() {
convertNumberBase();
return 0;
}
Binary file not shown.

0 comments on commit 3d860f5

Please sign in to comment.