This is a function of a string to int (libc has the same function Strtol series)
This is a toy function, but at the beginning of the design, the following goals are determined:
1. Function prototypes mimic strtol, but remove strtol second argument
int Str2Int (char *str, int base); Toy function prototype
Long strtol(const char *restrict str , char **restrict endptr, int base);//LIBC library function prototype
2. Expand the Strtol function (environment for Mac LIBC) of the binary guessing ability, when the base is 0 o'clock Strtol function will be based on the restric prefix 0x is inferred as 16, 0 is 8, the toy function added 0b prefix to indicate 2 binary
3. Support for the binary between 2-36. For example: Z is 35 in 10 binary
4. Support for negative numbers
5. Test the maximum value that 4 bytes can represent, minimum value, including 2,8,10,16 binary
Code
#include <stdio.h> #include <ctype.h>/* * @param base Can be any integer between 0 or 2-36 * if it is 0, the function will guess the binary to go, if Str starts with 0x, the equivalent base is 16 * if STR is the beginning of 0b, the equivalent base is 2 * If STR is beginning with 0, the equivalent base is 8 */ int str2int (Char *str, int base) { char sign; int rv = 0; char c; int newbase = -1; if (base < 2 && base != 0) | | &NBSP;BASE&NBSP;>&NBSP;36) return 0; //Skip the white space character at the beginning of str while (*str && isspace (*STR)) str++; //Get sign bit sign = (* str == '-' | | *str == ' + ') ? *str++ : ‘ + '; //guess the type of conversion required if (*str == ' 0 ' && &NBSP;++STR) { if (*str == ' x ' | | *str == ' X ') && ++str { newbase = 16; } else if (*str == ' B ' | | *str == ' B ') && ++str { newbase = 2; } else { newbase = 8; } } //default 10 binary if (base == 0) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; base = newbase == -1 ? 10 : newbase; } //printf ("base = %d:%s\n", base, str); for (; *str; str++) { //uppercase to lowercase c = *str | 0x20; if (c >= ' a ' && c <= ' z ') { rv = rv * base + c - ' A ' + 10; } else if (c > = ' 0 ' && c <= ' 9 ') { rv = rv * base + c - ' 0 '; &nbsP;} else { break; } //printf ("rv = %d\n ", &NBSP;RV); } return sign == '-' ? -rv : rv;} #include <stdlib.h> #include <limits.h> #include <assert.h>int main () &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;//TEST&NBSP;&NBSP;&NBSP;&NBSP;//====&NBSP;MAX=====&NBSP;&NBSP;&NBSP;&NBSP;//2 in the system assert (Str2Int ("0b11111111111111111111111111111111", 0) == 0xFFFFFFFF); printf ("2 max:%x\n", str2int ("0b11111111111111111111111111111111", 0)); printf ("2 max:%x\n", str2int ("0b11111111111111111111111111111111", 2)); assert (Str2Int ("0777777777777", 0) == 0xfFFFFFFF); //8 printf ("8 max:%x\n", str2int (" 0777777777777 ", 0)); printf (" 8 max:%u\n ", str2int (" 0777777777777 ", 0)); printf ("8 max:%x\n", str2int ("0777777777777", 8)); printf ("8 max:%u\n", str2int ("0777777777777", 8)); assert ( Str2Int ("4294967295", 0) == 0xffffffff); &NBSP;&NBSP;&NBSP;&NBSP;//10 printf ("10 max:%u\n", str2int ("4294967295", 0)); printf ("10 max:%u\ N ", str2int (" 4294967295 ", 10)); assert (Str2Int (" 0xFFFFFFFF ", 0) == &NBSP;0XFFFFFFFF); //16 printf ("16 max:%x\n", Str2Int ("0xFFFFFFFF", 0)), printf ("16 max:%x\n", str2int ("0XFFFFFFFf", 0)); printf ("16 max:%x\n", str2int ("0xFFFFFFFF", 16)); printf ("16 max:%x\ N ", str2int (" 0XFFFFFFFf ", 16)); assert (Str2Int (" 0b10000000000000000000000000000000 ", 0) == 0x80000000); //=====min===== &NBSP;&NBSP;&NBSP;&NBSP;//2 binary printf ("2 min:%x\n", str2int (" 0b10000000000000000000000000000000 ", 0)); printf (" 2 min:%x\n ", str2int (" 0b10000000000000000000000000000000 ", 2)); printf (" 2 min:%d\n ", str2int (" 0b10000000000000000000000000000000 ", 0)); printf (" 2 min:%d\n ", str2int (" 0b10000000000000000000000000000000 ", 2)); assert (Str2Int (" 020000000000 ", 0) == 0x80000000); //8 printf ("8 min:%d\n", Str2Int ("020000000000", 8)); &NBSP;&NBSP;&NBSP;&NBSP;PRintf ("8 min:%d\n", str2int ("020000000000", 0)); printf ("8 min:%x\n") , str2int ("020000000000", 8)), printf ("8 min:%x\n", str2int (" 020000000000 ", 8)); &NBSP;&NBSP;&NBSP;&NBSP;//10 printf (" 10 min:%d\n ", Str2Int (" -2147483648", 10)); printf ("10 min:%x\n", str2int ("2147483648") , 10)); &NBSP;&NBSP;&NBSP;&NBSP;//16 printf ("16 min:%d\n", str2int (" 0x80000000 ", 16)); printf (" 16 min:%x\n ", str2int (" 0x80000000 ", 16)) ; //printf ("%x", strtoul (" -2147483648", null, ); //35 binary printf ("35:%d\n", str2int ("Z", 35)); return 0;}
C Language String to number