# C Program to Convert Hexadecimal to Decimal

## Introduction

The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems. Low-level memory access, a small collection of keywords, and a clean style are all qualities that make C language excellent for system programming, such as operating system or compiler development.

## Why Learn C Programming Language?

Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.

Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.

## Hexadecimal and Decimal Number System

The American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication.  ASCII is an 8-bit code standard that assigns letters, numerals, and other characters to the 256 slots available. For example, the decimal value of the lower case “h” character (Char) is 104, which is “01101000” in binary and “68” in hexadecimal.
The Base of 16 is used in the “Hexadecimal” or simply “Hex” numbering system. To represent all integers, hexadecimal numerals employ 16 symbols: 0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. (A, B, C, D, E, F) are the letters for the following digits (10, 11, 12, 13, 14, 15).

### Decimal Number System

The decimal number system employs ten symbols to represent numbers with a base of ten: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. It’s also known as the Hindu-Arabic number system, in which each digit has a location and is ten times more important than the one before it. A decimal point is also used to denote decimal fractions.

## Conversion of Hexadecimal to Decimal

The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a Hexadecimal to Decimal converter using c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>

#define ARRAY_SIZE  20
int main()
{
char hex[ARRAY_SIZE];
long long decimal = 0, base = 1;
int i = 0, value, length;
// Get hexadecimal input value from user
fflush(stdin);
fgets(hex,ARRAY_SIZE,stdin);
// Calculating the string length of the given hexadecimal input by the user
length = strlen(hex);
// Hexadecimal numbers are 0 - F
// i.e 0 - 9 and A - F
for(i = length--; i >= 0; i--)
{
// Incase the user inputs (0 - 9)
if(hex[i] >= '0' && hex[i] <= '9')
{
decimal += (hex[i] - 48) * base;
base *= 16;
}
// Incase the user inputs (A - F)
else if(hex[i] >= 'A' && hex[i] <= 'F')
{
decimal += (hex[i] - 55) * base;
base *= 16;
}
// Incase the user inputs (a - f)
else if(hex[i] >= 'a' && hex[i] <= 'f')
{
decimal += (hex[i] - 87) * base;
base *= 16;
}
}
printf("\nThe Hexadecimal Number = %s", hex);
printf("The Converted Decimal number = %lld\n", decimal);
return 0;
}

## Output

• Output Screen Shot 1 :
• Output Screen Shot 2 :
• Output Screen Shot 3 :

SHARE C Program to Convert Hexadecimal to Decimal

#### You may also like... 