2020-05-28 06:22:49 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Convert decimal number to hexadecimal representation
|
|
|
|
*/
|
2016-10-14 16:12:54 +08:00
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
#include <iostream>
|
2016-10-14 16:12:54 +08:00
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
/**
|
|
|
|
* Main program
|
|
|
|
*/
|
|
|
|
int main(void) {
|
|
|
|
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
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
std::cout << "Enter a Decimal Value"
|
|
|
|
<< std::endl; // Displays request to stdout
|
|
|
|
std::cin >>
|
|
|
|
valueToConvert; // Stores value into valueToConvert via user input
|
2016-10-14 16:12:54 +08:00
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
while (valueToConvert > 15) { // Dec to Hex Algorithm
|
|
|
|
hexArray[i++] = valueToConvert % 16; // Gets remainder
|
|
|
|
valueToConvert /= 16;
|
|
|
|
// valueToConvert >>= 4; // This will divide by 2^4=16 and is faster
|
|
|
|
}
|
|
|
|
hexArray[i] = valueToConvert; // Gets last value
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
std::cout << "Hex Value: ";
|
|
|
|
while (i >= 0) std::cout << HexValues[hexArray[i--]];
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2020-05-28 06:22:49 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
return 0;
|
2016-10-14 16:12:54 +08:00
|
|
|
}
|