TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
decimal_to_hexadecimal.cpp File Reference

Convert decimal number to hexadecimal representation. More...

#include <iostream>
Include dependency graph for decimal_to_hexadecimal.cpp:

Go to the source code of this file.

Functions

int main (void)
 

Detailed Description

Convert decimal number to hexadecimal representation.

Definition in file decimal_to_hexadecimal.cpp.

Function Documentation

◆ main()

int main ( void )

Main program

Definition at line 11 of file decimal_to_hexadecimal.cpp.

11 {
12 int valueToConvert = 0; // Holds user input
13 int hexArray[8]; // Contains hex values backwards
14 int i = 0; // counter
15 char HexValues[] = "0123456789ABCDEF";
16
17 std::cout << "Enter a Decimal Value"
18 << std::endl; // Displays request to stdout
19 std::cin >>
20 valueToConvert; // Stores value into valueToConvert via user input
21
22 while (valueToConvert > 15) { // Dec to Hex Algorithm
23 hexArray[i++] = valueToConvert % 16; // Gets remainder
24 valueToConvert /= 16;
25 // valueToConvert >>= 4; // This will divide by 2^4=16 and is faster
26 }
27 hexArray[i] = valueToConvert; // Gets last value
28
29 std::cout << "Hex Value: ";
30 while (i >= 0) std::cout << HexValues[hexArray[i--]];
31
32 std::cout << std::endl;
33 return 0;
34}