From 67d692597a8c184df0f30557799e60aa6db21f50 Mon Sep 17 00:00:00 2001 From: wwells4 Date: Fri, 14 Oct 2016 01:12:54 -0700 Subject: [PATCH] Create Decimal To Hex .cpp --- Decimal To Hexadecimal .cpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Decimal To Hexadecimal .cpp diff --git a/Decimal To Hexadecimal .cpp b/Decimal To Hexadecimal .cpp new file mode 100644 index 000000000..fadddfd74 --- /dev/null +++ b/Decimal To Hexadecimal .cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +void main(void) +{ + int valueToConvert = 0; //Holds user input + int hexArray[8]; //Contains hex values backwards + int i = 0; //counter + int lValue = 0; //Last Value of Hex result + + cout << "Enter a Decimal Value" << endl; //Displays request to stdout + cin >> valueToConvert; //Stores value into valueToConvert via user input + + while (valueToConvert > 0) //Dec to Hex Algorithm + { + lValue = valueToConvert % 16; //Gets remainder + valueToConvert = valueToConvert / 16; + hexArray[i] = lValue; //Stores converted values into an array + i++; + } + cout << "Hex Value: "; + while (i > 0) + { + //Displays Hex Letters to stdout + switch (hexArray[i - 1]) { + case 10: + cout << "A"; + break; + case 11: + cout << "B"; + break; + case 12: + cout << "C"; + break; + case 13: + cout << "D"; + break; + case 14: + cout << "E"; + break; + case 15: + cout << "F"; + break; + default: + cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value + } + i--; + } + cout << endl; +}