Difference between revisions of "Convert string to integer"
From GbdevWiki
Line 8: | Line 8: | ||
(at your option) any later version. | (at your option) any later version. | ||
− | Convert string to interger | + | Convert string to interger is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Latest revision as of 13:46, 23 May 2015
/*Copyright Tom Lukeywood 2014 This file is part of Convert string to interger. Convert string to interger is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Convert string to interger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Convert string to interger edition. If not, see <http://www.gnu.org/licenses/>. */ unsigned int atoi(char* s, unsigned char len); unsigned int atoi(char* s, unsigned char len){ unsigned int val=0; unsigned int flag=0; unsigned char i; for(i=0;i<len;i++) { if(s[i] >= '0' && s[i] <= '9'){val = val * 10 + s[i] -'0';} else if(s[0] == '-' && i==0){flag = 1;} else{break;} } if(flag == 1) val = val * -1; return val; }
example of the function being used:
char c[10] = "123"; int i = atoi(c, 3); printf("%d", i);