C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C solved programs » Advance C programs

C program to convert hexadecimal Byte to integer

This program will convert a hexadecimal value in integer. The logic behind to implement this program - separate both character that a hexadecimal value contains, and get their integer values and then multiply with 16 (as hexadecimal value’s base is 16) and then add second value, simply you will integer value.
For example: Let suppose a value “9A” (Hexadecimal value in string) and output will be 154 (an integer value).

Convert hexadecimal Byte to integer using C program

/*C program to convert hexadecimal Byte to integer.*/

#include <stdio.h>

//function : getNum
//this function will return number corresponding

int getNum(char ch)
    int num=0;
    if(ch>='0' && ch<='9')
            case 'A': case 'a': num=10; break;
            case 'B': case 'b': num=11; break;
            case 'C': case 'c': num=12; break;
            case 'D': case 'd': num=13; break;
            case 'E': case 'e': num=14; break;
            case 'F': case 'f': num=15; break;
            default: num=0;
    return num;

//function : hex2int
//this function will return integer value against
//hexValue - which is in string format

unsigned int hex2int(unsigned char hex[])
    unsigned int x=0;
int main()
    unsigned char hexValue[2]="9A";
    unsigned int  intValue=0; //can be stored in unsigned char

    printf("value is: %d\n",intValue);
    return 0;


    value is: 154

Liked this article? Do share with your friends :)