This tutorial will be over how to convert decimal (base 10) to binary (base 2) and hexadecimal (base 16). If you want to catch up on my other tutorials in this series, here they are:
Part 11: Colored Text in your Terminal
Part 16: Binary, Bitwise Operators, and 2^n
Part 19: Object Oriented Programming 1 -- Data Structures
Part 20: Object Oriented Programming 2 -- Classes
Part 21: Object Oriented Programming 3 -- Polymorphism
Part 22: Command Line Arguments
Part 24: Programming in separate files
Part 26: Threading and concurrency
Binary is a numbering system that only uses two numbers, 0 and 1. Here are some examples of decimal numbers in binary.
0 = 0
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 1000
Here is another way to look at it:
0 = (0)2^0
------- 0 -- = 0
1 = (1)2^0
------- 1 -- = 1
2 = (1)2^1 +(0)2^0
------- 1 -------- 0 -- = 10
3 = (1)2^1 +(1)2^0
------- 1 -------- 1 -- = 11
4 = (1)2^2 (0)2^1 +(0)2^0
------- 1 ------- 0--------0 -- = 100
... and so on...
To convert a decimal number to binary, simply divide the number by 2 until it is 0, then list the remainders backwards.
Example:
14
14 / 2 = 7 --- r 0
7 / 2 = 3 --- r 1
3 / 2 = 1 --- r 1
1 / 1 = 0 --- r 1
= 1110
Hexadecimal is a numbering system that uses 16 numbers. Since there are no numerical digits past 9, letters are used instead. Here are some examples of decimal numbers in hexadecimal.
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
...
9 = 9
10 = A
11 = B
12 = C
13 = D
14 = E
15 = F
16 = 10
17 = 11
Here is another way to look at it:
0 = (0)16^0
------- 0 -- = 0
1 = (1)16^0
------- 1 -- = 1
...
17 = (1)16^1 +(1)16^0
--------- 1 --------- 1 -- = 11
18 = (1)16^1 +(2)16^0 = 12
--------- 1 --------- 2 -- = 12
...
30 = (1)16^1 (14)16^0
--------- 1 --------- 14 --- = 1E
... and so on...
Converting decimal numbers to Hexadecimal is similar to binary. Just replace the 2 with a 16.
Example:
45
45 / 16 = 2 --- r 13
2 / 16 = 0 --- r 2
= 2D
Here is a program I wrote that converts a decimal number to binary and hexadecimal:
#include
#include
using namespace std;
void toBinary(int n)
{
int binary[32] = {0};
binary[0] = n % 2;
for(int x = 1; n != 0; x++)
{
n = n / 2;
binary[x] = n % 2;
}
bool skip = false;
for(int x = 31; x >= 0; x--)
{
if(binary[x] == 1)
skip = true;
if(skip)
cout << binary[x];
}
cout << endl;
}
void toHex(int n)
{
int hex[8] = {0};
hex[0] = n % 16;
for(int x = 1; n != 0; x++)
{
n = n / 16;
hex[x] = n % 16;
}
bool skip = false;
for(int x = 7; x >= 0; x--)
{
if(hex[x] != 0)
skip = true;
if(skip)
{
if(hex[x] == 10)
cout << "A";
else if(hex[x] == 11)
cout << "B";
else if(hex[x] == 12)
cout << "C";
else if(hex[x] == 13)
cout << "D";
else if(hex[x] == 14)
cout << "E";
else if(hex[x] == 15)
cout << "F";
else
cout << hex[x];
}
}
cout << endl;
}
int main()
{
cout << "Enter a number to convert to binary and hexidecimal: ";
int number;
cin >> number;
cout << number << " in binary is: ";
toBinary(number);
cout << number << " in hexidecimal is: ";
toHex(number);
return 0;
}
Here is some sample output:
[cmw4026@omega test]$ g++ conversions.cpp
[cmw4026@omega test]$ ./a.out
Enter a number to convert to binary and hexidecimal: 30
30 in binary is: 11110
30 in hexidecimal is: 1E
[cmw4026@omega test]$ ./a.out
Enter a number to convert to binary and hexidecimal: 78
78 in binary is: 1001110
78 in hexidecimal is: 4E
[cmw4026@omega test]$ ./a.out
Enter a number to convert to binary and hexidecimal: 4
4 in binary is: 100
4 in hexidecimal is: 4
[cmw4026@omega test]$ ./a.out
Enter a number to convert to binary and hexidecimal: 28
28 in binary is: 11100
28 in hexidecimal is: 1C
[cmw4026@omega test]$
I hope this helped! Leave any suggestions in the comments!