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

27 lines
704 B
C++
Raw Normal View History

2016-10-14 16:12:54 +08:00
#include <iostream>
using namespace std;
int main(void){
2016-10-14 16:12:54 +08:00
int valueToConvert = 0; //Holds user input
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
cin >> valueToConvert; //Stores value into valueToConvert via user input
while (valueToConvert > 15){ //Dec to Hex Algorithm
hexArray[i++] = valueToConvert % 16; //Gets remainder
valueToConvert /= 16;
2016-10-14 16:12:54 +08:00
}
hexArray[i] = valueToConvert; //Gets last value
2016-10-14 16:12:54 +08:00
cout << "Hex Value: ";
while (i >= 0)
cout<<HexValues[hexArray[i--]];
2016-10-14 16:12:54 +08:00
cout << endl;
return 0;
2016-10-14 16:12:54 +08:00
}