How to convert hexadecimal string to integer in C

3 Answers

0 votes
#include <stdio.h>

int main()
{
    char s[] = "FFFA0E1D";
    
    int n;
    sscanf(s, "%x", &n);
    
    printf("%u\n", n);
}
 
/*

run:

4294577693

*/

 



answered Jul 19, 2018 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char hex[] = "0x1AF0";
   
    int n = strtol(hex, 0, 16);
    
    printf("%d\n", n);
}
 
/*

run:

6869

*/

 



answered Jul 19, 2018 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char hex[] = "0x1AFD";
   
    int n = strtoul(hex, 0, 16);
    
    printf("%d\n", n);
}
 
/*

run:

6909

*/

 



answered Jul 19, 2018 by avibootz

Related questions

1 answer 244 views
1 answer 208 views
1 answer 208 views
1 answer 286 views
1 answer 159 views
4 answers 393 views
1 answer 110 views
...