# C++ Program to convert hexadecimal to decimal

C++ Program to convert hexadecimal to decimal with std::hex

```/**
* Converting Hexadecimal to Decimal in C++  codebind.com
*
*/

#include <iostream>
int main()
{
int integer;
std::cout<<"Entex Hex to Convert hex to decimal"<<std::endl;
std::cin >> std::hex >> integer;
std::cout << integer << std::endl;
return 0;
}

/*
OUTPUT:
Entex Hex to Convert hex to decimal
10
16
*/
```

C++ Program to convert hexadecimal to decimal without std::hex

```/**
* Converting Hexadecimal to Decimal in C++  codebind.com
*
*/

#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

unsigned long HexToDec(char hex[]) {
char *hexstr;
int length = 0;
const int base = 16;     // Base of Hexadecimal Number
unsigned long decnum = 0;
int i;

// Now Find the length of Hexadecimal Number
for (hexstr = hex; *hexstr != '\0'; hexstr++) {
length++;
}

hexstr = hex;
for (i = 0; *hexstr != '\0' && i < length; i++, hexstr++) {
// Compare *hexstr with ASCII values
if (*hexstr >= 48 && *hexstr <= 57) {  // is *hexstr Between 0-9
decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
}
else if ((*hexstr >= 65 && *hexstr <= 70))  {  // is *hexstr Between A-F
decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
}
else if (*hexstr >= 97 && *hexstr <= 102) {  // is *hexstr Between a-f
decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
}
else {

}
}
return decnum;
}

void main() {
unsigned long decnum;
char hex[9];     // 8 characters for 32-bit Hexadecimal Number and one for ' '

std::cout<<" Enter 32-bit Hexadecimal Number : ";
std::cin>>hex;

decnum = HexToDec(hex);
std::cout<<"Value in Decimal Number is "<<decnum<<"\n";

getch();
}

/*
OUTPUT:
Enter 32-bit Hexadecimal Number : 10
Value in Decimal Number is 16

*/
```

# Partner Sites

## SqliteTutorials.com

#### 1 Comment

1. but its valid only upto 13 characters, what if i want to give around 30 35 characters for input…

kindly help me out for this.