Skip to content

Commit 16846f7

Browse files
Merge pull request #1 from wwells4/patch-1
Create Decimal To Hex .cpp
2 parents 3465588 + 67d6925 commit 16846f7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Decimal To Hexadecimal .cpp

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

0 commit comments

Comments
 (0)