# Program for hexadecimal to decimal

Given a hexadecimal number as input, we need to write a program to convert the given hexadecimal number into equivalent decimal number.

Examples:

```Input : 67
Output : 55

Input : 512
Output : 330

Input: 123
Output: 83
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We know that in hexadecimal number uses 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all numbers. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15).
The idea is to extract the digits of given hexadecimal number starting from right most digit and keep a variable dec_value. At the time of extracting digits from the hexadecimal number, multiply the digit with the proper base (Power of 16) and add it to the variable dec_value. At the end, the variable dec_value will store the required decimal number.

For Example:
If the hexadecimal number is 1A.
dec_value = 1*(16^1) + 10*(16^0) = 26

Below diagram explains how to convert hexadecimal number ( 1AB ) to equivalent decimal value:

Below is the implementation of above idea.

## C/C++

```// C++ program to convert hexadecimal to decimal
#include<iostream>
#include<string.h>
using namespace std;

// Function to convert hexadecimal to decimal
{
int len = strlen(hexVal);

// Initializing base value to 1, i.e 16^0
int base = 1;

int dec_val = 0;

// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;

// incrementing base by power
base = base * 16;
}

// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;

// incrementing base by power
base = base*16;
}
}

return dec_val;
}

// Driver program to test above function
int main()
{
char hexNum[] = "1A";
return 0;
}
```

## Java

```// Java program to convert hexadecimal to decimal
import java.io.*;

class GFG
{
// Function to convert hexadecimal to decimal
{
int len = hexVal.length();

// Initializing base value to 1, i.e 16^0
int base = 1;

int dec_val = 0;

// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value
if (hexVal.charAt(i) >= '0' && hexVal.charAt(i) <= '9')
{
dec_val += (hexVal.charAt(i) - 48)*base;

// incrementing base by power
base = base * 16;
}

// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal.charAt(i) >= 'A' && hexVal.charAt(i) <= 'F')
{
dec_val += (hexVal.charAt(i) - 55)*base;

// incrementing base by power
base = base*16;
}
}
return dec_val;
}

// driver program
public static void main (String[] args)
{
String hexNum = "1A";
}
}

// Contributed by Pramod Kumar
```

Output:

```26
```

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1 Average Difficulty : 1/5.0
Based on 1 vote(s)