Created
September 26, 2013 10:19
-
-
Save ormaaj/6712291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <limits.h> | |
| #include <errno.h> | |
| #define SIZE 4 | |
| int main(int argc, char **argv) { | |
| char *basic = "8887"; | |
| char *dataStr = "1234 1a2b3c4e -10101010 0xdeadbeef"; | |
| char *endPtr = dataStr; | |
| int bases[] = {10, 16, 2, 0}; | |
| long long i = 0, d[SIZE]; | |
| // Basic conversion | |
| fprintf(stdout, "Basic string: %lld\n", strtoll(basic, NULL, 10)); | |
| // Using the endPtr | |
| while (*endPtr != '\0') { | |
| d[i] = strtoll(endPtr, &endPtr, bases[i]); | |
| fprintf(stdout, "%lld, %ld\n", i++, endPtr - dataStr); | |
| } | |
| fprintf(stdout, "%lld, %lld, %lld, %lld\n", d[0], d[1], d[2], d[3]); | |
| return 0; | |
| } | |
| /* | |
| *Basic string: 8887 | |
| *0, 4 | |
| *1, 13 | |
| *2, 23 | |
| *3, 34 | |
| *1234, 439041102, -170, 3735928559 | |
| */ | |
| /* vim: set fenc=utf-8 ff=unix ft=c : */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment