Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to convert hexadecimal byte to integer in C

1 Answer

0 votes
#include <stdio.h>

int getNum(char ch) {
    int n = 0;
    if (ch >= '0' && ch <= '9') {
        n = ch - 0x30;
    }
    else {
        switch(ch) {
            case 'A': case 'a': n = 10; break;
            case 'B': case 'b': n = 11; break;
            case 'C': case 'c': n = 12; break;
            case 'D': case 'd': n = 13; break;
            case 'E': case 'e': n = 14; break;
            case 'F': case 'f': n = 15; break;
            default: n = 0;
        }
    }
    return n;
}
 
unsigned int hex2int(unsigned char hex[]) {
    return (getNum(hex[0])) * 16 + (getNum(hex[1]));
}

int main()
{
    unsigned char hex[2] = "A0";
    unsigned int  i;
 
    i = hex2int(hex);
     
    printf("%d\n", i);
     
    return 0;
}
 
/*

run:

160

*/

 



answered Jul 19, 2018 by avibootz

Related questions

3 answers 319 views
2 answers 352 views
3 answers 219 views
219 views asked Oct 28, 2016 by avibootz
8 answers 776 views
1 answer 78 views
...