How to parse string to with different base numbers in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

int main() {
    char s[] = "32 1AF 1101 0x3ff -101101";
    char *pEnd;
    long int li1, li2, li3, li4, li5;
  
    li1 = strtol(s, &pEnd, 10);
    li2 = strtol(pEnd, &pEnd, 16);
    li3 = strtol(pEnd, &pEnd, 2);
    li4 = strtol(pEnd, &pEnd, 16);
    li5 = strtol(pEnd, NULL, 2);
    
    printf("%ld, %ld, %ld, %ld, %ld\n", li1, li2, li3, li4, li5);
}
 
/*

run:

32, 431, 13, 1023, -45

*/

 



answered Jul 20, 2018 by avibootz
edited Jan 3, 2021 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

int main() {
    char arr[] = "98807231 1FA3 1110 0xFF2C";
    char* pEnd;
    long long ll1, ll2, ll3, ll4;
  
    ll1 = strtoll(arr, &pEnd, 10);
    ll2 = strtoll(pEnd, &pEnd, 16);
    ll3 = strtoll(pEnd, &pEnd, 2);
    ll4 = strtoll(pEnd, NULL, 0);
    
    printf ("%lld, %lld, %lld, %lld", ll1, ll2, ll3, ll4);
     
    return 0;
}
 
 
 
/*
run:
 
98807231, 8099, 14, 65324
 
*/

 



answered Jan 3, 2021 by avibootz

Related questions

1 answer 120 views
120 views asked Jan 2, 2021 by avibootz
2 answers 287 views
1 answer 207 views
...