Skip to content

Conversion Operations

Learn how to perform basic mathematical conversion operations for different values such as binary to decimal, or hex to octal.
Written by: Brianna Laird
Last updated: December 2024


Converting between different numerical and data representations is an essential skill in programming, allowing for efficient data processing, interoperability, and the ability to build versatile applications. This guide will explore how to validate, convert, encode, and decode various formats, making your programs both robust and flexible. These concepts also serve as a foundation for implementing more complex systems in SplashKit, such as cryptographic utilities or network tools.

Validation Functions

Validation is the first step in any reliable conversion process. Ensuring input values conform to their respective formats reduces errors and creates a solid foundation for processing data. For example, binary numbers must consist only of 0s and 1s, while hexadecimal values use a combination of numeric and alphabetic characters. In this section, we’ll explore how to check the correctness of binary, decimal, hexadecimal, and octal values, ensuring all inputs are clean and error-free before proceeding with conversions.

Is Binary

This function checks if a given string is a valid binary number, ensuring it contains only the characters 0 and 1. It also verifies that the input is not empty, returning false for any invalid or empty input.

#include <iostream>
#include <string>
using namespace std;
bool is_binary(const string &binary_string)
{
for (char character : binary_string)
{
if (character != '0' && character != '1')
return false;
}
return !binary_string.empty();
}

Is Hexadecimal

This function validates whether a string represents a valid hexadecimal number. It checks that each character falls within the ranges 0-9, A-F, or a-f, while also ensuring the input is not empty.

bool is_hex(const string &hex_string)
{
for (char character : hex_string)
{
if (!((character >= '0' && character <= '9') || (character >= 'A' && character <= 'F') || (character >= 'a' && character <= 'f')))
return false;
}
return !hex_string.empty();
}

Is Octal

This function determines if a string is a valid octal number by ensuring all characters are within the range 0-7. It also returns false if the input string is empty.

bool is_octal(const string &octal_string)
{
for (char character : octal_string)
{
if (character < '0' || character > '7')
return false;
}
return !octal_string.empty();
}

Basic Conversion Operations

Conversions between number systems are fundamental to understanding how data is represented in programming. Whether you’re transforming binary to decimal, decimal to hexadecimal, or even converting between less common systems like octal and base-64, these operations provide insight into the underlying mechanics of data storage and manipulation. This section will introduce methods for performing these conversions step by step, helping you understand the relationships between different numerical systems and how to implement them efficiently in code.

Decimal to Binary

Converting a decimal number to binary involves dividing the number by 2 repeatedly and recording the remainder for each division. These remainders, read in reverse order, form the binary representation of the decimal number. For example, to convert the decimal number 10 to binary, you would perform the following steps: remainder , remainder , remainder , and remainder . Reading the remainders from bottom to top, you get the binary number . This process can be implemented programmatically to convert any decimal number to its binary equivalent.

string dec_to_bin(unsigned int decimal_value)
{
if (decimal_value == 0)
return "0";
string binary_string;
while (decimal_value > 0)
{
binary_string = ((decimal_value & 1) ? "1" : "0") + binary_string;
decimal_value >>= 1;
}
return binary_string;

Using this function

To use this function in your code, you can call it with a decimal value as an argument. For example, to convert the decimal number 10 to binary, you would call the function as follows:

unsigned int decimal_value = 10;
string binary_string = dec_to_bin(decimal_value);
cout << "Binary value: " << binary_string << endl;

Expected Output

The output of the above code will be:

Binary value: 1010

If we change the input value to , the output will be:

Binary value: 10110

Binary to Decimal

Converting a binary number to decimal involves multiplying each digit by , where is the position of the digit from the rightmost bit, starting at . For example, to convert the binary number to decimal, you would calculate . This process can be implemented programmatically to convert any binary number to its decimal equivalent.

unsigned int bin_to_dec(const string &binary_string)
{
if (!is_binary(binary_string))
{
return 0;
}
unsigned int decimal_value = 0;
for (size_t i = 0; i < binary_string.size(); i++)
{
if (binary_string[i] == '1')
{
decimal_value += (1 << (binary_string.size() - i - 1));
}
}
return decimal_value;
}

Using this function

To use this function in your code, you can call it with a binary value as an argument. For example, to convert the binary number to decimal, you would call the function as follows:

string binary_string = "1010";
unsigned int decimal_value = bin_to_dec(binary_string);
cout << "Decimal value: " << decimal_value << endl;

Expected Output

The output of the above code will be:

Decimal value: 10

If we change the input value to , the output will be:

Decimal value: 22

Hexadecimal to Binary

Converting a hexadecimal number to binary involves converting each hexadecimal digit to its binary equivalent. For example, the hexadecimal number can be converted to binary by converting to and to , resulting in the binary number . This process can be implemented programmatically to convert any hexadecimal number to its binary equivalent.

string hex_to_bin(const string &hex_string)
{
if (!is_hex(hex_string))
{
return "";
}
string binary_string;
for (char hex_character : hex_string)
{
int hex_value = 0;
if (hex_character >= '0' && hex_character <= '9')
hex_value = hex_character - '0';
else if (hex_character >= 'A' && hex_character <= 'F')
hex_value = hex_character - 'A' + 10;
else if (hex_character >= 'a' && hex_character <= 'f')
hex_value = hex_character - 'a' + 10;
for (int i = 3; i >= 0; i--)
{
binary_string += ((hex_value >> i) & 1) ? '1' : '0';
}
}
if (hex_string.length() == 1)
{
size_t first_one = binary_string.find_first_not_of('0');
return (first_one == string::npos) ? "0" : binary_string.substr(first_one);
}
return binary_string;
}

Using this function

To use this function in your code, you can call it with a hexadecimal value as an argument. For example, to convert the hexadecimal number to binary, you would call the function as follows:

string hex_string = "A3";
string binary_string = hex_to_bin(hex_string);
cout << "Binary value: " << binary_string << endl;

Expected Output

The output of the above code will be:

Binary value: 10100011

If we change the input value to , the output will be:

Binary value: 1111

Binary to Hexadecimal

Converting a binary number to hexadecimal involves grouping the binary digits into sets of four, starting from the rightmost bit. Each group of four bits corresponds to a single hexadecimal digit, allowing you to convert the binary number to its hexadecimal equivalent. For example, the binary number can be grouped as and , which correspond to the hexadecimal digits and , respectively. This process can be implemented programmatically to convert any binary number to its hexadecimal equivalent.

string bin_to_hex(const string &binary_string)
{
if (!is_binary(binary_string))
{
return "";
}
string hex_string;
int length = binary_string.length();
int padding = (4 - (length % 4)) % 4;
string padded_binary_string = string(padding, '0') + binary_string;
for (size_t i = 0; i < padded_binary_string.length(); i += 4)
{
int hex_value = 0;
for (size_t j = 0; j < 4; j++)
{
hex_value <<= 1;
if (padded_binary_string[i + j] == '1')
hex_value |= 1;
}
if (hex_value < 10)
hex_string += '0' + hex_value;
else
hex_string += 'A' + (hex_value - 10);
}
return hex_string;
}

Using this function

To use this function in your code, you can call it with a binary value as an argument. For example, to convert the binary number to hexadecimal, you would call the function as follows:

string binary_string = "10100011";
string hex_string = bin_to_hex(binary_string);
cout << "Hexadecimal value: " << hex_string << endl;

Expected Output

The output of the above code will be:

Hexadecimal value: A3

If we change the input value to , the output will be:

Hexadecimal value: F

Decimal to Octal

Converting a decimal number to octal involves dividing the number by 8 repeatedly and recording the remainder for each division. These remainders, read in reverse order, form the octal representation of the decimal number. For example, to convert the decimal number 10 to octal, you would perform the following steps: remainder , and remainder . Reading the remainders from bottom to top, you get the octal number . This process can be implemented programmatically to convert any decimal number to its octal equivalent.

string dec_to_oct(unsigned int decimal_value)
{
if (decimal_value == 0)
return "0";
string octal_string;
while (decimal_value > 0)
{
octal_string = to_string(decimal_value % 8) + octal_string;
decimal_value /= 8;
}
return octal_string;
}

Using this function

To use this function in your code, you can call it with a decimal value as an argument. For example, to convert the decimal number 10 to octal, you would call the function as follows:

unsigned int decimal_value = 10;
string octal_string = dec_to_oct(decimal_value);
cout << "Octal value: " << octal_string << endl;

Expected Output

The output of the above code will be:

Octal value: 12

If we change the input value to , the output will be:

Octal value: 26

Octal to Decimal

Converting an octal number to decimal involves multiplying each digit by , where is the position of the digit from the rightmost bit, starting at . For example, to convert the octal number 12 to decimal, you would calculate . This process can be implemented programmatically to convert any octal number to its decimal equivalent.

unsigned int oct_to_dec(const string &octal_string)
{
if (!is_octal(octal_string))
{
return 0;
}
unsigned int decimal_value = 0;
for (size_t i = 0; i < octal_string.size(); i++)
{
decimal_value = decimal_value * 8 + (octal_string[i] - '0');
}
return decimal_value;
}

Using this function

To use this function in your code, you can call it with an octal value as an argument. For example, to convert the octal number 12 to decimal, you would call the function as follows:

string octal_string = "12";
unsigned int decimal_value = oct_to_dec(octal_string);
cout << "Decimal value: " << decimal_value << endl;

Expected Output

The output of the above code will be:

Decimal value: 10

If we change the input value to , the output will be:

Decimal value: 22

Octal to Binary

Converting an octal number to binary involves converting each octal digit to its binary equivalent. For example, the octal number can be converted to binary by converting to and to , resulting in the binary number . This process can be implemented programmatically to convert any octal number to its binary equivalent.

string oct_to_bin(const string &octal_string)
{
if (!is_octal(octal_string))
{
return "";
}
string binary_string;
for (char octal_character : octal_string)
{
int octal_value = octal_character - '0';
for (int i = 2; i >= 0; i--)
{
binary_string += ((octal_value >> i) & 1) ? '1' : '0';
}
}
size_t first_one = binary_string.find_first_not_of('0');
return (first_one == string::npos) ? "0" : binary_string.substr(first_one);
}

Using this function

To use this function in your code, you can call it with an octal value as an argument. For example, to convert the octal number 12 to binary, you would call the function as follows:

string octal_string = "12";
string binary_string = oct_to_bin(octal_string);
cout << "Binary value: " << binary_string << endl;

Expected Output

The output of the above code will be:

Binary value: 1010

If we change the input value to , the output will be:

Binary value: 10110

Binary to Octal

Converting a binary number to octal involves grouping the binary digits into sets of three, starting from the rightmost bit. Each group of three bits corresponds to a single octal digit, allowing you to convert the binary number to its octal equivalent. For example, the binary number can be grouped as and , which correspond to the octal digits and , respectively. This process can be implemented programmatically to convert any binary number to its octal equivalent.

string bin_to_oct(const string &binary_string)
{
if (!is_binary(binary_string))
{
return "";
}
string octal_string;
int padding = (3 - (binary_string.length() % 3)) % 3;
string padded_binary_string = string(padding, '0') + binary_string;
for (size_t i = 0; i < padded_binary_string.length(); i += 3)
{
int octal_value = 0;
for (size_t j = 0; j < 3; j++)
{
octal_value <<= 1;
if (padded_binary_string[i + j] == '1')
octal_value |= 1;
}
octal_string += '0' + octal_value;
}
size_t first_non_zero = octal_string.find_first_not_of('0');
return (first_non_zero == string::npos) ? "0" : octal_string.substr(first_non_zero);
}

Using this function

To use this function in your code, you can call it with a binary value as an argument. For example, to convert the binary number to octal, you would call the function as follows:

string binary_string = "101010";
string octal_string = bin_to_oct(binary_string);
cout << "Octal value: " << octal_string << endl;

Expected Output

The output of the above code will be:

Octal value: 52

If we change the input value to , the output will be:

Octal value: 26

Hexadecimal to Octal

Converting a hexadecimal number to octal involves converting the hexadecimal number to binary and then converting the binary number to octal. This process can be implemented programmatically to convert any hexadecimal number to its octal equivalent.

string hex_to_oct(const string &hex_string)
{
if (!is_hex(hex_string))
{
return "";
}
string binary_string = hex_to_bin(hex_string);
return bin_to_oct(binary_string);
}

Using this function

To use this function in your code, you can call it with a hexadecimal value as an argument. For example, to convert the hexadecimal number to octal, you would call the function as follows:

string hex_string = "A3";
string octal_string = hex_to_oct(hex_string);
cout << "Octal value: " << octal_string << endl;

Expected Output

The output of the above code will be:

Octal value: 123

If we change the input value to , the output will be:

Octal value: 17

Octal to Hexadecimal

Converting an octal number to hexadecimal involves converting the octal number to binary and then converting the binary number to hexadecimal. This process can be implemented programmatically to convert any octal number to its hexadecimal equivalent.

string oct_to_hex(const string &octal_string)
{
if (!is_octal(octal_string))
{
return "";
}
string binary_string = oct_to_bin(octal_string);
return bin_to_hex(binary_string);
}

Using this function

To use this function in your code, you can call it with an octal value as an argument. For example, to convert the octal number 12 to hexadecimal, you would call the function as follows:

string octal_string = "12";
string hex_string = oct_to_hex(octal_string);
cout << "Hexadecimal value: " << hex_string << endl;

Expected Output

The output of the above code will be:

Hexadecimal value: A

If we change the input value to , the output will be:

Hexadecimal value: 16

Summary

This guide showed you how to convert numbers between different number systems, including decimal, binary, octal, and hexadecimal. You learned how to implement conversion functions in C++, C#, and Python, and how to use these functions to convert numbers between different number systems. By following the examples and explanations provided in this guide, you should now have a better understanding of how to convert numbers between different number systems programmatically. You can use this knowledge to implement number system conversion functions in your own projects and applications.