Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <limits.h>
- long strtol(char* nPtr, char **endPtr , int base)
- {
- char *string;
- long returnedNumber = 0;
- int numberSign;
- assert(nPtr); /* quits if string is empty */
- string = nPtr; /* string points to the same place as nPtr*/
- while(isspace(* string)) /* omitt white numbers*/
- {
- string++;
- }
- if(*string =='-')
- {
- numberSign = 1;
- string++;
- }
- else
- {
- if(*string =='+')
- {
- numberSign = 0;
- string++;
- }
- else numberSign = 0;
- }
- if((base == 8) && (*string =='0'))
- {
- string++;
- }
- if((base == 16) && (*string =='0'))
- {
- string++;
- if((*string =='x')||(*string =='X'))
- {
- string++;
- }
- else
- {
- string--;
- }
- }
- if((*string =='0') && (base == 0))
- {
- base = 8;
- string++;
- if((*string =='x')||(*string =='X'))
- {
- base = 16;
- string++;
- }
- }
- else if(base == 0) /*if the base is 0 and the beginning of string is not 0, 0x or 0X then the base is 10*/
- {
- base = 10;
- }
- if(base < 2 || base > 36)
- {
- errno = EINVAL;
- return 0;
- }
- if((base == 8)&&((*string < '0')||(*string > '7')))
- {
- *endPtr = string;
- return 0;
- }
- if((base == 16) && ((*string < '0')||((*string >'9')&&(*string < 'A'))||((*string > 'F')&&(*string < 'a'))||(*string > 'f')))
- {
- string--;
- *endPtr = string;
- return 0;
- }
- return returnedNumber;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement