text to binary and text to hexadecimal
Converting Text to Binary and Text to Hexadecimal in C++

Creating a C++ program that can translate text to binary and text to hexadecimal is a good way to understand how these two number bases work . Making this program can also help with understanding how for loops work and serve as an introduction to the bitset library and the std::hex object.

Getting Started

The first thing you will need to do is include the following three libraries in your main.cpp file:

#include <iostream>
#include <string>
#include <bitset>

Declaring the Necessary Functions

Next after including the necessary libraries you will want to declare the following three functions, they will make up the main menu, the text to binary translator and the text to hexadecimal translator.

//The Three Functions the Program Needs

void menu(); 
void texttobinary();
void texttohex();

Creating the Main Menu Function

After writing your function declarations create your int main() function which should only consist of one line, a call to the main menu function.

// Calls the Main Menu Function
int main()
{
    menu();
}

Now write the definition for the void mainmenu() function. First declare the string variable which will contain the users input. After this you need to prompt the user for which conversion they want to do and then grab the request with std::cin. You will need to call std::ignore(‘) after getting the user input to clear the cin buffer. If you don’t make sure the buffer is clear, the program will end up breaking later on when the user tries to enter in a phrase for translation.

//Determines which translator the user wants to use and runs it 

std::string request; 

std::cout << "Do You Wish to Convert Text to Binary or text to Hexadecimal? (B/H) \n";

std::cin >> request;

std::cin.ignore();

Before moving on you’ll want to implement a simple check to determine if the user entered in the correct values. This should consist of a simple while statement that keeps repeating for as long as they enter an incorrect value. In this while statement clear the cin buffer and make sure to reprompt the user for input.

//Checks to make sure the user entered valid input
//If input is invalid user will be stuck in a repeating loop until valid input is given

while (request!="B"&&!="H")
{
     std::cout << "Invalid Input Please Try Again\n"; 

     std::cin.clear(); 

     std::cin.ignore(256,'\n'); 

     std::cin >> request;

     std::cin.ignore ();
}

Now check what selection the user made and run the corresponding function.

//User is directed to their chosen translator

if (request == "B")
{
     texttobinary();
}

else if (request == "H")
{
     texttohex();
}

Text to Binary Function

You should begin the function by defining two string variables. One for holding the users input and another for what they decide to do after the translation is finished. After this you need to prompt the user for the phrase they wish to have translated and grab it using std::getline(std::cin,input) . The reason for using getline instead of std::cin is that the latter stops at the first space it encounters whilst getline gets the entire line regardless of spaces, this allows users to enter in phrases as well as words.

//Collects the string for the text to binary function
//Uses Getline to make sure every word is captured and not just the first

void texttobinary()
{
     std::string input; 

     std::string cont;

     std::cout << "Enter The Text You Wish to Translate into Binary \n";

     getline(std::cin, input);

After this you need to create a for loop that will iterate through the text, convert it into binary and display it to the screen. This is made possible by std::bitset which comes from the bitset library you included at the start. You have to simply print the conversion of each character to the screen and each letter will be printed on its own line. Set the size of the bitset to 8, the size of one byte of data.

//A For statement iterates through the string
//Each letter is converted into its binary equivalent using bitset
//Prints text to binary translation to the screen

for (size_t i = 0; i < input.size(); ++i) 
{
     std::cout<< std::bitset(input.c_str()[i]) << std::endl; 
}

After the translation is printed add a short prompt that asks the user if they wish to end the program or rerun it.

//Asks the user if they want to close the program or continue
//Includes a while loop for checking errors

std::cout << "Would You Like to Return to the Main Menu, Or Exit the Program (M/E)\n"; 

std::cin >> cont;

while (cont!="M"&&cont!="E")
{
     std::cout << "Invalid Input Please Try Again\n";
 
     std::cin.clear();

     std::cin.ignore(256,'\n'); 

     std::cin >> cont;
}

if (cont=="M") 
{
    mainmenu();
}

else if(cont=="E")
{

}

Text to Hexadecimal Function

The beginning of this function is somewhat similar to the text to binary function, you must define the same variables and prompt the user to input a phrase for translation. The actual text must be changed though to reflect that this is the hexadecimal function and not the binary one.

//Collects the string for the text to hexadecimal function
//Uses Getline to make sure every word is captured and not just the first one

std::string input;

std::string cont; 

std::cout << "Enter The Text You Wish to Translate into Hexadecimal \n";

getline(std::cin, input); 

Now you need to create a for loop that iterates through the users entered string. As the string is iterated through each of its individual characters must be converted into a char variable. Std::hex is used to translate text to hexadecimal but it doesn’t work with strings which is why we needed to make each character a char.

//Iterates through the text string with a for loop
//Converts each character of the string into a char
//each char is then converted into its hex equivalent using std::hex
//Prints text to hexadecimal translation to the screen

for (int i = 0; i < input.length(); i++)
{ 
     char letter = input.at(i); 

     std::cout << std::hex << int(letter); 
}

This ends in the same way as the text to binary function so feel free to just copy paste the ending of that function here and just change the wording to reflect that this is the hexadecimal translation function.

//Asks the user if they want to close the program or continue
//Includes a while loop for checking errors

std::cout << "\nWould You Like to Return to the Main Menu, Or Exit the Program (M/E)\n";

std::cin >> cont;

while (cont!="M"&&cont!="E")
{
     std::cout<<"Invalid Input Please Try Again\n"; 

     std::cin.clear(); 

     std::cin.ignore(256,'\n'); 

     std::cin >> cont; 
}

if (cont=="M") 
{
     mainmenu();
}

else if(cont=="E") 
{

}

Further Suggestions

Some things you can add to the program for a bit of extra challenge are: functionality to reverse the process, additional mathematical bases, and the ability to save the original text and its translation to a file so it can be viewed later.

Good Luck!

Download the above Source code here.

If you have any comments or questions contact me at nick@crumbsofcode.com