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

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.