Changeset 426 for trunk/libs/stdlib.c
- Timestamp:
- Jan 29, 2018, 6:00:54 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libs/stdlib.c
r416 r426 22 22 */ 23 23 24 #include <nostdio.h>25 24 #include <stdio.h> 26 25 #include <stdarg.h> … … 40 39 } 41 40 42 ///////////////////////// 43 int atoi(const char *str) 44 { 45 int res = 0; // Initialize result 46 int sign = 1; // Initialize sign as positive 47 int i = 0; // Initialize index of first digit 48 49 if (str[0] == '-') //If number is negative, then update sign 50 { 51 sign = -1; 52 i++; // Also update index of first digit 53 } 54 55 for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result 56 { 57 res = res*10 + str[i] - '0'; 41 ////////////////////////// 42 int atoi(const char * str) 43 { 44 int res = 0; // Initialize result 45 int sign = 1; // Initialize sign as positive 46 int i = 0; // Initialize index of first digit 47 48 if( (str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')) ) // hexa 49 { 50 i = 2; 51 52 while( str[i] != 0 ) 53 { 54 if ( (str[i] >= '0') && (str[i] <= '9') ) res = (res<<4) + (str[i] - '0'); 55 else if( (str[i] >= 'A') && (str[i] <= 'F') ) res = (res<<4) + (str[i] - 'A'); 56 else if( (str[i] >= 'a') && (str[i] <= 'f') ) res = (res<<4) + (str[i] - 'a'); 57 else return 0; 58 i++; 59 } 60 } 61 else // decimal 62 { 63 if (str[0] == '-') // number is negative, update sign 64 { 65 sign = -1; 66 i++; // Also update index of first digit 67 } 68 69 while( str[i] != 0 ) 70 { 71 if( (str[i] >= '0') && (str[i] <= '9') ) res = (res*10) + (str[i] - '0'); 72 else return 0; 73 i++; 74 } 58 75 } 59 76 … … 382 399 if ( count == -1 ) 383 400 { 384 panic( "stdlibxprintf failure" );401 display_string( "stdlib : xprintf failure" ); 385 402 return -1; 386 403 } … … 495 512 } // end getint() 496 513 497 /////////////////////////////////////// ////498 int snprintf( char * string,514 /////////////////////////////////////// 515 int snprintf( char * string, 499 516 unsigned int length, 500 const char * format, ... )517 const char * format, ... ) 501 518 { 502 519 va_list args;
Note: See TracChangeset
for help on using the changeset viewer.