Easy Tutorial: Computer Programming for DUMMIES -- converting decimal to binary and hexidecimal

in #programming8 years ago (edited)

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 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

Part 14: 2D Arrays

Part 15: String Processing

Part 16: Binary, Bitwise Operators, and 2^n

Part 17: Pointers

Part 18: Pointer Arithmetic

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 23: Header Files

Part 24: Programming in separate files

Part 25: File I/O

Part 26: Threading and concurrency

Binary

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

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<iostream>
#include<iostream>
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;
}
  • toBinary() --> converts a given decimal number to binary and prints it
    • n --> the decimal number passed to the function
    • binary --> a integer array to hold the binary digits backwards
    • stores the remainder of n / 2 in binary then divides n by 2
    • skip --> a boolean that holds true whenever the first value in the array binary shows up that isn't 0
      • since all of the values in binary were initialized to 0, there are a bunch of unnecessary 0s that need to be skipped
    • once the first 1 is reached, start printing the array
      • remember, it is printing in reverse order
  • toHex() --> converts a given decimal number to hexadecimal and prints it
    • n --> the decimal number passed to the function
    • hex --> an integer array that holds the digits of the hexadecimal number
    • stores each remainder of n / 16 in hex
    • divides n by 16 until it is 0
    • skip --> a boolean that holds true whenever the first value in the array hex shows up that isn't 0
    • if the number is not between 0 and 9, print the corresponding letter
    • if the number is 0 - 9, print that number
    • this array is printed backwards just like in toBinary()
  • main()
    • prompt user to enter a number
    • number --> an integer to hold that given number
    • print the binary and hexadecimal equivalent by calling the two functions

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!


Sort:  

Hello @charlie.wilson,

It gives us pleasure to inform you that this post have been upvoted by Project Better.

The Mission of Project Better is to reward posts have many votes from Minnows but earn pennies.
Your payout is $0.082 before we vote on your post.

Learn more about the Project Better here!

We hope to see you continuing to post some great stuff on Steemit!

Good luck!

~BETTER~

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57497.94
ETH 3099.12
USDT 1.00
SBD 2.32