TheAlgorithms-C-Plus-Plus/others/Decimal To Hexadecimal .cpp

29 lines
675 B
C++
Raw Normal View History

2016-10-14 16:12:54 +08:00
#include <iostream>
using namespace std;
2019-08-21 10:10:08 +08:00
int main(void)
{
2016-10-14 16:12:54 +08:00
int valueToConvert = 0; //Holds user input
2019-08-21 10:10:08 +08:00
int hexArray[8]; //Contains hex values backwards
int i = 0; //counter
char HexValues[] = "0123456789ABCDEF";
2016-10-14 16:12:54 +08:00
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
2019-08-21 10:10:08 +08:00
cin >> valueToConvert; //Stores value into valueToConvert via user input
2016-10-14 16:12:54 +08:00
2019-08-21 10:10:08 +08:00
while (valueToConvert > 15)
{ //Dec to Hex Algorithm
hexArray[i++] = valueToConvert % 16; //Gets remainder
valueToConvert /= 16;
2016-10-14 16:12:54 +08:00
}
2019-08-21 10:10:08 +08:00
hexArray[i] = valueToConvert; //Gets last value
2016-10-14 16:12:54 +08:00
cout << "Hex Value: ";
while (i >= 0)
2019-08-21 10:10:08 +08:00
cout << HexValues[hexArray[i--]];
2016-10-14 16:12:54 +08:00
cout << endl;
return 0;
2016-10-14 16:12:54 +08:00
}