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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

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 54 views
54 views asked Jan 2, 2021 by avibootz
2 answers 94 views
1 answer 94 views
...