# Thread: Converting decimal to hex

1. ## Converting decimal to hex

I've been writing a program that will convert a decimal input to an octal or hex number. Basically I just need some help with the decimal to hex conversion. I searched the board for a while, but couldnt find anything...(I'm new to C++, so it might be that i just dont understand it.) Here's my code so far, thanks for any help!

Code:
```#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
enum ConverstionType {NONE,DECIMAL,HEX,OCTAL};
int number = 0;// a global variable visible throughout the file
int test = NONE;

// this function converts a char to upper case
// and display it if not a digit representation;
void asciiToInt(char aChar){
int digitValue = 0;
switch(::test) {
case OCTAL: {
digitValue = aChar - '0';
::number *= 8;
::number += digitValue;
break;
}
case DECIMAL:
{
digitValue = aChar - '0';
::number *= 10;
::number += digitValue;
break;
}
case HEX:
{if (aChar >= 'a' && aChar <= 'f')
digitValue = aChar - 'a' + 10;
else
digitValue = aChar - '0';
::number *= 16;
::number += digitValue;
break;
}

default: {
cout << "error in conversion" << endl;
::number = 0;
}
}
}

int main(){
string word;
cout << "enter a number:";
getline(cin,word);
if(word[0] == '0')
::test = OCTAL;
else if(word[1] == 'x')
::test = HEX;
else
::test = DECIMAL;
cout << word << endl;

// apply the function showChar to each character in the string
for_each(word.begin(),word.end(),asciiToInt);
cout << "converted number is " << number << endl;
return 0;
}```

long number;
std::cout << "Please enter a number: ";
std::cin >> number;

std::cout << "\nIn hex: " << std::hex << number;
std::cout << "\nIn oct: " << std::oct << number;
std::cout << "\nIn dec << std::dec << number;

3. Thanks a lot! Thats a whole lot easier. I just dont know if my professor will take it. Know of a way to do it with the code that i started with??

4. Well, you're going to need a carry value from time to time when converting from a higher to lower base... or if you're converting from lower to higher base you'll need a borrow. You could search planet-source-code.com for examples of converting between hexadecimal/decimal/octal

5. Thanks for the help. There's a lot of good stuff on there too. Can someone tell me how to use a carry/borrow value? Like I said, I'm really new to this.

6. What do you mean by "carry and borrow"

7. Thats the problem...I dont really know. Refer to Eibro's post.

8. Originally posted by Mister C
What do you mean by "carry and borrow"
Nevermind, I guess I didn't look at his code closely enough.

9. >>Know of a way to do it with the code that i started with??
Yes, I also know of a better way. Here's a function that does what you want :-)
Code:
```#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string hex_value(int decimal)
{
static const string hex_digits("0123456789ABCDEF");

string hex;
int scratch = decimal;

while (scratch != 0)
{
hex += hex_digits[scratch % 16];
scratch /= 16;
}

reverse(hex.begin(), hex.end());

return hex;
}

int main()
{
cout<< hex_value(27) <<endl;
}```

10. Thanks for everyone's help. I finally got it working using the form my professor wants. If anybody's interested, here's my final code. Thanks again.

Code:
```#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
enum ConverstionType {NONE,DECIMAL,HEX,OCTAL};
int number = 0;// a global variable visible throughout the file
int test = NONE;

// this function converts a char to upper case
// and display it if not a digit representation;
void asciiToInt(char aChar){
int digitValue = 0;
switch(::test) {
case OCTAL: {
digitValue = aChar - '0';
::number *= 8;
::number += digitValue;
break;
}
case DECIMAL:
{
digitValue = aChar - '0';
::number *= 10;
::number += digitValue;
break;
}
case HEX:
{if (aChar >= 'a' && aChar <= 'f')
digitValue = aChar - 'a' + 10;
else if (aChar == 'x')
digitValue = 0;
else
digitValue = aChar - '0';
::number *= 16;
::number += digitValue;
break;
}

default: {
cout << "error in conversion" << endl;
::number = 0;
}
}
}

int main(){
string word;
cout << "Enter a number to convert to decimal form:" << endl;
cout << "Octal numbers should be preceded by a 0." << endl;
cout << "Hexadecimal numbers should be preceded by an x." << endl;

getline(cin,word);
if(word[0] == '0')
::test = OCTAL;
else if(word[0] == 'x')
::test = HEX;
else
::test = DECIMAL;
cout << word << endl;
// apply the function showChar to each character in the string
for_each(word.begin(),word.end(),asciiToInt);
cout << "The Converted number is " << number << endl;
cout << " " << endl;

return 0;
}```