forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decimal To Hexadecimal .cpp
51 lines (47 loc) · 1.09 KB
/
Decimal To Hexadecimal .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
void main(void)
{
int valueToConvert = 0; //Holds user input
int hexArray[8]; //Contains hex values backwards
int i = 0; //counter
int lValue = 0; //Last Value of Hex result
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
cin >> valueToConvert; //Stores value into valueToConvert via user input
while (valueToConvert > 0) //Dec to Hex Algorithm
{
lValue = valueToConvert % 16; //Gets remainder
valueToConvert = valueToConvert / 16;
hexArray[i] = lValue; //Stores converted values into an array
i++;
}
cout << "Hex Value: ";
while (i > 0)
{
//Displays Hex Letters to stdout
switch (hexArray[i - 1]) {
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
default:
cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value
}
i--;
}
cout << endl;
}