Easy Tutorial: Computer Programming for DUMMIES -- converting decimal to binary and hexidecimal by charlie.wilson

View this thread on steempeak.com
· @charlie.wilson · (edited)
$3.79
Easy Tutorial: Computer Programming for DUMMIES -- converting decimal to binary and hexidecimal
http://previews.123rf.com/images/dr911/dr9111202/dr911120200027/12377615-a-blue-binary-codes-background-Stock-Photo-binary-matrix.jpg

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!**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming)

[**Part 2: Variables**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies)

[**Part 3: Functions**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies)

[**Part 4: if(), else, else if()**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners)

[**Part 5: Loops**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly)

[**Part 6: Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly)

[**Part 7: Basic Input/Output**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly)

[**Part 9: Sorting Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting)

[**Part 10: Random Numbers**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers)

[**Part 11: Colored Text in your Terminal**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal)

[**Part 12: Recursion**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple)

[**Part 13: Binary Search**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search)

[**Part 14: 2D Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy)

[**Part 15: String Processing**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings)

[**Part 16: Binary, Bitwise Operators, and 2^n**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-bitwise-operators-and-2-n)

[**Part 17: Pointers**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointers)

[**Part 18: Pointer Arithmetic**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointer-arithmetic)

[**Part 19: Object Oriented Programming 1 -- Data Structures**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-1-data-structures)

[**Part 20: Object Oriented Programming 2 -- Classes**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-2-classes)

[**Part 21: Object Oriented Programming 3 -- Polymorphism**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-3-polymorphism)

[**Part 22: Command Line Arguments**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-command-line-arguments)

[**Part 23: Header Files**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-header-files)

[**Part 24: Programming in separate files**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-programming-in-separate-files)

[**Part 25: File I/O**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-100-followers-edition-file-i-o)

[**Part 26: Threading and concurrency**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-concurrency-and-pthreads)

# 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!

---

<center>https://i.imgsafe.org/5b80091fff.png</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 117 others
properties (23)
post_id1,270,544
authorcharlie.wilson
permlinkeasy-tutorial-computer-programming-for-dummies-converting-decimal-to-binary-and-hexidecimal
categoryprogramming
json_metadata"{"links": ["https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-bitwise-operators-and-2-n", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointers", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-pointer-arithmetic", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-1-data-structures", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-2-classes", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-object-oriented-programming-3-polymorphism", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-command-line-arguments", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-header-files", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-programming-in-separate-files", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-100-followers-edition-file-i-o", "https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-concurrency-and-pthreads"], "image": ["http://previews.123rf.com/images/dr911/dr9111202/dr911120200027/12377615-a-blue-binary-codes-background-Stock-Photo-binary-matrix.jpg"], "tags": ["programming", "tutorial", "convert", "binary", "hexadecimal"]}"
created2016-10-30 17:36:30
last_update2016-10-30 17:37:51
depth0
children1
net_rshares20,675,491,557,169
last_payout2016-11-30 19:45:36
cashout_time1969-12-31 23:59:59
total_payout_value2.948 SBD
curator_payout_value0.837 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length8,941
author_reputation18,764,341,719,560
root_title"Easy Tutorial: Computer Programming for DUMMIES -- converting decimal to binary and hexidecimal"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (181)
@better ·
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!](https://steemit.com/robots/@better/better-30-10-2016)

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

Good luck!

~BETTER~
👍  
properties (23)
post_id1,270,685
authorbetter
permlinkre-easy-tutorial-computer-programming-for-dummies-converting-decimal-to-binary-and-hexidecimal
categoryprogramming
json_metadata{}
created2016-10-30 18:05:36
last_update2016-10-30 18:05:36
depth1
children0
net_rshares12,031,620,135
last_payout2016-11-30 19:45:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length448
author_reputation15,727,752,514,706
root_title"Easy Tutorial: Computer Programming for DUMMIES -- converting decimal to binary and hexidecimal"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)