15,789,700 members
See more:
Trying to create a program that would convert a decimal into a hexadecimal string without using std::hex. When conversion occurs, it returns a flipped version of the correct answer for the Hexadecimal

Am unable to think of a solution for this problem.

```int i;
int temp;
string hex;
long totalDecimal;
cin >> totalDecimal;

for (i = 0; totalDecimal != 0; i++)
{
temp = totalDecimal % 16;

if (temp > 10)
{
hex += temp + 55;
}
else
{
hex += temp + 48;
}
totalDecimal = totalDecimal / 16;
}

cout << hex;
return 0;
```

What I have tried:

making hex store an array e.g hex[100] and using for loops to assign values to each element in the array, but it only made it much more confusing and complicated
Posted
Updated 10-Oct-20 17:07pm

## Solution 1

You have two options:
1) Store the hex digits and display them in reverse later.
2) Work from the most significant digit first.

Option2 isn't as bad as it sounds, not realy.
1) Set up a boolean called "leadingZero" and set it to true
2) Set up a shift count, set it to 28 (for 32 bit numbers, 60 for 64 bit)
3) Loop though all digits.
3.1) Shift the input by shift count bits, using the >> operator and mask it to a digit:
C++
`digit = (input >> shiftCount) & 0xF;`

3.2) If the digit not zero or leadignZero is false print the digit. Set leading Zero to false
3.3) Reduce shiftCount by 4 and loop back to 3.1

Member 13899738 5-Jul-18 9:00am
I appreciate the 2 different options you gave! However, I am trying to make the function return the hexadecimal digits instead of printing them out in reverse. As for the second option, I have not learnt how bit shifting would work and preferably would not want to use it until I learn it. Thank you!!

## Solution 2

Let's look at what happens:

Iteration 1:
totalDecimal = 100
totalDecimal%16 = 4
hex = "4"

Iteration 2:
totaldecimal = 6 (100/16)
totalDecimal%16 = 6
hex = "46"

So every iteration, you divide by 16 (going right to left), but you add to the end of your string (left to right).
You could fix it by adding to your string from right to left:

```string hex = "";
...
if (temp > 10)
{
hex.insert(0, 1, temp + 55);
}
else
{
hex.insert(0, 1, temp + 48);
}```

v3
Member 13899738 5-Jul-18 8:58am
Thank you for the response! However, when I try to use hex = (temp + 55) + hex, the compiler states that there are "no operator '+' matches these operands". How can I resolve this issue?
[no name] 5-Jul-18 9:00am
You're right, it's been a while since I typed C++ :) Please see my edit.
`hex = char(temp + 55) + hex;`
or...
just do this at the end of your original solution.
`hex = std::reverse(hex);`
You also need `#include <algorithm>`

## Solution 3

Assuming you want to print out a hex string of the decimal number I default to using sprintf() to stuff a char array with the hex number using the %x specifier.

This is the old way and probably not what you superiors are after out of you but it will get you what you want - I think.

## Solution 4

```#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string hex;
unsigned long l;
cin >> l;

if ( l == 0 )
hex = "0";
else
{
while (l)
{
unsigned char c = l & 0x0F;
hex += static_cast<char>( c < 10 ? c + '0' : c - 10 + 'A' );
l >>= 4;
}
reverse( hex.begin(), hex.end());
}
cout << hex << endl;
}```

## Solution 5

```/*  decimal /hexa
Jose Anilto
*/
#include <iostream>
#include <cstring>

using namespace std;

string converte(int n, string x) {

string hexa = {"0123456789ABCDEF"};
return (n < 16 ? "0x" + (hexa[n] + x) : converte(n/16, hexa[n%16] + x));

}

int main () {

int nd ;
string y = "";
cout << "\n Digite um numero: "; cin >> nd;
cout << "\n Numero digitado: " << nd << "\n";

cout << "\n Hexadecimal: " << converte(nd, y) << "\n";

return 0;
}
```

v2