Base Convertor project using C++

Base Convertor project using C++ (With source code)

Base Convertor project using C++ (With source code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a Base Convertor project using C++ (With source code).

A Base Converter application written in C++ is a program which offers a menu of binary conversion options. This project has base conversion mechanism. It has functions for the conversions to take place. Any number may be entered and converted to any number system (for example, decimal to binary, binary to Hexadecimal, and so on).

Base Convertor project using C++

This basic utility converts the same number across its representations in several number systems. Furthermore, the design of this system is rather straightforward, so the user will encounter no issues when working on it.

Bank Management System using C++ (With Source Code)

How does the Base Convertor program work?

The program’s primary function provides the user with a menu and accepts a number from the menu selections. The specified conversion is then carried out until the user inputs 5 to quit the program.

Overview of the UI:

Binary Bunch Conversions
     1. Convert base 10 to binary
     2. Convert binary to base 10
     3. Convert base 16 to binary
     4. Convert binary to base 16
     5. End Program
The user will choose an option to perform the conversion operation of their choice. User will then enter a value they want to convert. 

Base Convertor project using C++Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
using namespace std;


string base2To16Table(string num)
{
    map<string, string> table;
    string conversion;

    table.insert(pair<string,string> ("0000", "0"));
    table.insert(pair<string,string> ("0001", "1"));
    table.insert(pair<string,string> ("0010", "2"));
    table.insert(pair<string,string> ("0011", "3"));
    table.insert(pair<string,string> ("0100", "4"));
    table.insert(pair<string,string> ("0101", "5"));
    table.insert(pair<string,string> ("0110", "6"));
    table.insert(pair<string,string> ("0111", "7"));
    table.insert(pair<string,string> ("1000", "8"));
    table.insert(pair<string,string> ("1001", "9"));
    table.insert(pair<string,string> ("1010", "A"));
    table.insert(pair<string,string> ("1011", "B"));
    table.insert(pair<string,string> ("1100", "C"));
    table.insert(pair<string,string> ("1101", "D"));
    table.insert(pair<string,string> ("1110", "E"));
    table.insert(pair<string,string> ("1111", "F"));

    conversion = table.find(num)->second;
    return conversion;
}

string base16To2Table(char num)
{
    
    map<char, string> table;
    string conversion;

    table.insert(pair<char, string> ('0', "0000"));
    table.insert(pair<char, string> ('1', "0001"));
    table.insert(pair<char, string> ('2', "0010"));
    table.insert(pair<char, string> ('3', "0011"));
    table.insert(pair<char, string> ('4', "0100"));
    table.insert(pair<char, string> ('5', "0101"));
    table.insert(pair<char, string> ('6', "0110"));
    table.insert(pair<char, string> ('7', "0111"));
    table.insert(pair<char, string> ('8', "1000"));
    table.insert(pair<char, string> ('9', "1001"));
    table.insert(pair<char, string> ('A', "1010"));
    table.insert(pair<char, string> ('B', "1011"));
    table.insert(pair<char, string> ('C', "1100"));
    table.insert(pair<char, string> ('D', "1101"));
    table.insert(pair<char, string> ('E', "1110"));
    table.insert(pair<char, string> ('F', "1111"));
    
    conversion = table.find(num)->second;
    return conversion;
}

int main(int argc, char* arv[])
{
    cout << "Binary Bunch Conversions" << endl;
    cout << "1. Convert base 10 to binary" << endl;
    cout <<	"2. Convert binary to base 10" << endl;
    cout <<	"3. Convert base 16 to binary" << endl;
    cout << "4. Convert binary to base 16" << endl;
    cout << "5. End Program" << endl;
    
    int input;
    //take in a number for the menu option
    cout << "Please Enter a Number from the Menu: ";
    cin >> input;

    //run until 5 is entered
    while (input != 5)
    {
        //Converting base 10 to binary						
        if (input == 1)
        {
            int num;
            string remainder;

            //read in a base 10 number
            cout << "Please Enter a Base 10 Number: ";
            cin >> num;

            //keep track of the orignial number
            int originalNum = num;

            //divide the number by two until you reach zero 
            //and keep track of the remainders and reverse it
            while(num != 0)
            {
                //track remainder
                int remain = num % 2;

                //convert the remainder int into a string
                stringstream ss;
                ss << remain;
                remainder += ss.str();
                
                //divide the base 10 number by 2 until zero
                num /= 2;
            }

            //take the remainder string and reverse it to get the binary
            //number
            reverse(remainder.begin(), remainder.end());

            //print out the original base 10 number and the base 2 equivalent
            cout << originalNum << " in base 2 is " << remainder << endl << endl;
        }

        //Convert binary to base 10
        else if (input == 2)
        {
            int base10Num;
            string base2Num;

            //read in a base 2 number 
            cout << "The format for a base 2 number should be in groups of 4 with no spaces" << endl;
            cout << "Example: 000110100011 = 419"<< endl;
            cout << "Please Enter a Base 2 Number: ";
            cin >> base2Num;

            //the power is how many digits the base 2 
            //number has minus one
            int power = base2Num.size()-1;

            //perform base expansion on the base 2 number
            for (int i = 0; (unsigned)i < base2Num.size(); i++)
            {
                //convert the char from its ascii value to 
                //its integer value
                int num = base2Num[i]-48;

                //base 10 equals the digit times 2 to the x power
                base10Num += num*pow(2,power);

                //decrement the power
                power--;
            }

            //print out the original base 2 number and the base 10 equivalent
            cout << base2Num << " in base 10 is " << base10Num << endl << endl;
        }

        //Convert base 16 to binary
        else if (input == 3)
        {
            string base2Num;
            string base16Num;

            //read in a base 16 number 
            cout << "Please Enter a Base 16 Number: ";
            cin >> base16Num;

            //search the lookup table for base 16 to 2 and concatenate the 
            //return strings
            for (int i = 0; (unsigned)i < base16Num.size(); i++)
            {
                base2Num += base16To2Table(base16Num[i]);
            }
            //print out the original base 16 number and the base 2 equivalent
            cout << base16Num << " in base 2 is " << base2Num << endl << endl;

        }

        //Convert binary to base 16
        else if (input == 4)
        {
            string base2Num;
            string base16Num;
            string substring;

            //read in a base 2 number 
            cout << "The format for a base 2 number should be in groups of 4 with no spaces" << endl;
            cout << "Example: 000110100011 = 1A3"<< endl;
            cout << "Please Enter a Base 2 Number: ";
            cin >> base2Num;

            //the index position at 0 and increment by 4 after each substring
            //is ran through the look up table
            int i = 0;
            
            while((unsigned)i < base2Num.length())
            {
                int position = i;

                //split the base 2 string into groups of four using the 
                //substring function.
                substring = base2Num.substr(position, 4);

                //concatenate each returned value to create a base 16 num
                base16Num += base2To16Table(substring);

                //increment by 4 to get a new group of four
                i+=4;
            }

            //print out the original base 2 number and the base 16 equivalent
            cout << base2Num << " in base 16 is " << base16Num << endl << endl;

        }

        //handle all other inputs
        else
        {
            cout << input << " is not a valid option. Please Select Again!" << endl;
            cout << "Please Enter a Number from the Menu: ";
            cin >> input;
        }
        
        cout << "Binary Bunch Conversions" << endl;
        cout << "1. Convert base 10 to binary" << endl;
        cout <<	"2. Convert binary to base 10" << endl;
        cout <<	"3. Convert base 16 to binary" << endl;
        cout << "4. Convert binary to base 16" << endl;
        cout << "5. End Program" << endl;
    
        //take in a number for the menu option
        cout << "Please Enter a Number from the Menu: ";
        cin >> input;
    }
    cout << "Thank you for using Binary Bunch Conversions!" << endl;
}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream (input and output stream), fstream (file input and output), string (string manipulation), sstream (string stream operations), algorithm (algorithmic operations), math.h (mathematical operations), and map (storing key-value pairs).
  • Now we will create the functions:
  • The “base2To16Table” function will take in a string parameter called “num”, representing a 4-bit binary number. We will use a lookup table, implementing it by using a map to convert a 4-bit binary number to its equivalent base 16 representation. It will search the map for the given binary number and then return its base 16 equivalent.
  • The “base16To2Table” function will take in a char parameter called “num” which represents a hexadecimal digit. Again using a lookup table to convert a hexadecimal digit to its equivalent 4-bit binary representation. It will search the map for the given hexadecimal digit and return its base 2 equivalent.
  • Now the main() function will take 2 parameters. It will display a menu with 5 conversion options: convert base 10  to binary, convert binary to base 10, convert base 16 to binary, convert binary to base 16, or exit the program. The function will then ask the user to enter a number from the menu. The program executes the selected conversion until the user enters 5 to exit the program.
  • We will use while loops and if-else control statements for the conversion purpose and function calls.
  • Let’s say the user will choose option 1, then the program will ask the user to enter a base 10 number and the system will convert it to binary using repeated division by 2 and concatenation of the remainders. It will then reverse the resulting string and outputs the original base 10 number and its base 2 equivalent. That is how it will give the output to the user. We will define the functionalities for all the other options as well.
  • For option 2, which is converting binary to base 10, we will write the code asking the user to enter a base 2 number in groups of 4 with no spaces and then converting it to base 10 using base expansion. The system will then give output – the original base 2 number and its base 10 equivalent.
  • For option 3 which is converting base 16 to binary, we will ask the user to enter a base 16 number and system will convert it to binary using the “base16To2Table” function defined above. It will then output the original base 16 number and its base 2 equivalent.
  • For option 4 which is converting binary to base 16, we will ask the user to enter a base 2 number in groups of 4 with no spaces, and convert it to base 16 using the “base2To16Table” function. It will then output the original base 2 number and its base 16 equivalent.
  • The option 5 will exit from the program. This sums up our project of Base Convertor using C++.

Chess Game Using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Option 1 :

Base Convertor project using C++

Option 2:

Base Convertor project using C++

Option 3:

Base Convertor project using C++

Option 4:

Base Convertor project using C++

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Bakery Management System using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodeWithRandom



Leave a Reply