# How to convert Decimal to Hexadecimal in C++

In How to convert Decimal to Hexadecimal in C++ Here we are going to learn about two number systems one is Decimal and another is hexadecimal. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.

### How to convert Decimal to Hexadecimal in C++

Note: In this code we are using custom function strrevv() to reverse the binary string. But if you are running this code on windows platform then you can use strrev() function which is available in string.h.
strrev() is not available on Linux so we have to write our own custom strrevv().

```#include<iostream>
#include <string.h>

using namespace std;

char *strrevv(char *str)
{
char *ch1, *ch2;

if (! str || ! *str)
return str;

for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
char HEXNUMS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

long long decimalNum, tmpDecNo;
char hexNum[65];
int index, rem;

cout << "Enter any decimal number: ";
cin >> decimalNum;

tmpDecNo = decimalNum;

index = 0;

while(tmpDecNo !=0)
{
rem = tmpDecNo % 16;

hexNum[index] = HEXNUMS[rem];

tmpDecNo = tmpDecNo / 16;

index++;
}
hexNum[index] = '#include<iostream>
#include <string.h>
using namespace std;
char *strrevv(char *str)
{
char *ch1, *ch2;
if (! str || ! *str)
return str;
for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
char HEXNUMS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
long long decimalNum, tmpDecNo;
char hexNum[65];
int index, rem;
cout << "Enter any decimal number: ";
cin >> decimalNum;
tmpDecNo = decimalNum;
index = 0;
while(tmpDecNo !=0)
{
rem = tmpDecNo % 16;
hexNum[index] = HEXNUMS[rem];
tmpDecNo = tmpDecNo / 16;
index++;
}
hexNum[index] = '\0';
strrevv(hexNum);
cout << "\nThe Hexadecimal equivalent of Decimal number " << decimalNum << " is " << hexNum);
return 0;
}
';

strrevv(hexNum);

cout << "\nThe Hexadecimal equivalent of Decimal number " << decimalNum << " is " << hexNum);

return 0;
}
```

Output

```
Enter any decimal number: 26

The Hexadecimal equivalent of Decimal number 26 is 1A
```