Skip to content

Commit

Permalink
Initial commit of BinaryDecimalConverter program
Browse files Browse the repository at this point in the history
  • Loading branch information
AyuExecutes committed Nov 25, 2024
0 parents commit c4101a4
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
129 changes: 129 additions & 0 deletions BinaryDecimalConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

// Function to convert a decimal number (as a string) to binary
void decimalToBinary(string decimalInput);

// Function to convert a binary number (as a string) to decimal
void binaryToDecimal(string binaryInput);

// Function to validate if a string is a valid decimal number
bool isValidDecimal(const string& input);

// Function to validate if a string is a valid binary number
bool isValidBinary(const string& input);

int main(){
string input;

cout << "Enter a number: ";
cin >> input;

// Check if the input is a valid binary or decimal number, and convert it accordingly
if (isValidBinary(input)) {
binaryToDecimal(input);

} else if (isValidDecimal(input)) {
decimalToBinary(input);

} else {
cout << "This is not a valid number." << endl;
return 1;
}

return 0;
}

// Function to validate if a string is a valid decimal number
bool isValidDecimal(const string& input) {

// Check if the input length is greater than or equal to 8
if (input.length() >= 8){
return false;
}

// Check if all characters in the string are digits
for (char c : input) {
if (!isdigit(c)) {
return false;
}
}
return true;
}

// Function to validate if a string is a valid binary number
bool isValidBinary(const string& input) {

// Check if the input length is exactly 8 digits
if (input.length() != 8) {
return false;
}

// Check if all characters are either '0' or '1'
for (char c : input) {
if (c != '0' && c != '1') {
return false;
}
}
return true;
}

// Function to convert decimal (as a string) to binary
void decimalToBinary(string decimalInput){

// Convert the string to an integer
int num = stoi(decimalInput);
string binary;

// Check if the number is within the range 0 to 255. If not, exit the program.
if ((num < 0) || (num > 255)){
cout << "This decimal number is outside the range 0 to 255." << endl;
exit(1);
}

cout << "Converting decimal to binary..." << endl;

for (int i = 0; i < 8; i++){

// Check if the current bit is 1 or 0 and prepend it to the binary string
binary = (num % 2 == 1 ? "1" : "0") + binary;

// Add a space after every 4 bits for readability
if (i == 3){
binary = " " + binary;
}

// Divide the number by 2 to get the next bit
num /= 2;
}

// Output the binary number
cout << "The result is " << binary << endl;
}

// Function to convert binary (as a string) to decimal
void binaryToDecimal(string binaryInput){

// Validate if the input string contains only '0' and '1'
if (!isValidBinary(binaryInput)) {
cout << "This is not a valid binary number." << endl;
exit(1);
}

int decimalResult = 0;
int power = 7;

cout << "Converting binary to decimal... " << endl;

for (char c : binaryInput){
if (c == '1') {
decimalResult += pow(2, power);
}
power--;
}

// Output the decimal number
cout << "The result is " << decimalResult << endl;
}
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# BinaryDecimalConverter
A simple C++ program to convert between decimal and binary numbers.

## Features
- Convert decimal numbers (0-255) to 8-bit binary representation.
- Convert 8-bit binary numbers to decimal representation.
- Input validation to ensure correct number formats.

## Usage
1. Clone the repository:
git clone https://github.com/AyuExecutes/BinaryDecimalConverter

2. Compile the program:
g++ BinaryDecimalConverter.cpp -o BinaryDecimalConverter

3. Run the program:
./BinaryDecimalConverter

4. Follow the prompts to enter a number and see the conversion results.
Example:

Enter a number: 20
Converting decimal to binary...
The result is 0001 0100
Enter a number: 00010100
Converting binary to decimal...
The result is 20

0 comments on commit c4101a4

Please sign in to comment.