Author |
Topic: ASCII to INT (Read 1561 times) |
|
eviljed
Newbie


Posts: 16
|
 |
ASCII to INT
« on: Aug 7th, 2002, 3:42pm » |
Quote Modify
|
Here it is (in C). Not too advanced. Note that if the scanf and printf were taken out (you could make the input an arguement), then no libraries would be needed. I used stdio for convience (besides, atoi is in stdlib, and most worthwhile string functions are in string, and I use neither) #include <stdio.h> void main() { int return_value = 0; char *string; scanf("%s", string); while (1) { if ((int) string[0] >= 48 && (int) string[0] <= 57) { return_value = (return_value * 10) + ((int) string[0]) - 48; string += sizeof(char); } else break; } printf("%d\n", return_value); }
|
|
IP Logged |
|
|
|
Aaron
Newbie


Gender: 
Posts: 14
|
 |
uh....
« Reply #1 on: Aug 9th, 2002, 11:04am » |
Quote Modify
|
I think according to the problem, you need to define the int atoi(char* pStr) function somewhere. Here is a slightly more modular solution, with the added functionalities for dealing with '+' and '-': // library functions used only for the test driver. #include <stddef.h> #include <stdio.h> // for compilers that do not recognize the bool type // typedef int bool // tests if the given character is a digit ('0'-'9') bool isADigit(char ch) { return ((ch >= '0')&&(ch <= '9')); } // converts a single digit(char) into an integer value(int) int digitToInt(char digit) { return (int)(digit - '0'); } // converts a numerical string into an integer value. int atoi(char* str) { switch(str[0]) { // empty string case NULL: return 0; // numbers starting with a '-' case '-': return -atoi(str+1); // numbers starting with a '+' case '+': return atoi(str+1); // process number default: int n = 0; for(int i = 0; str[i] != NULL; i++) { if(isADigit(str[i])) n = (n * 10) + digitToInt(str[i]); else { break; } } return n; } } // test driver int main(int argc, char** argv) { if(argc == 2) { printf("the number read is \"%d\".\n", atoi(argv[1])); return 0; } else return 1; }
|
|
IP Logged |
|
|
|
Chris
Guest

|
Holy crap ... here's a simple (and completely untested) solution. It does _not_ handle +/-, exponential notation, etc, as per the specs. It does handle integer overflow, however, returning -1 (I have no idea what's wrong with the formatting ... yes, I understand proportional fonts): Code: int atoi( char *p ) { int i = 0, j; while( *p >= '0' && *p <= '9' ) { j = i * 10 + *p - '0'; if( j < i ) return -1; i = j; } return i; } |
| If you want to ignore overflow, you can inline this: Code: int i = 0; while( isnum( *p )) i = i * 10 + *p - '0'; |
| regards, Chris
|
|
IP Logged |
|
|
|
Chris
Guest

|
Yup ... don't test, don't actually read the first post (which is basically the same code here) and see after posting that in both loops there is a huge bug that should be '... *p++ - '0' ...' bah
|
|
IP Logged |
|
|
|
Krishna
Guest

|
one Simple soultion for Str to Int Conversion. didnt get a chance to test, I am sure it works. int AtoI( char *ptr) { int Neg=0; int Num=0; if(!ptr) return NULL; if(*ptr=='-') { Neg=1; ptr++; } while(*ptr) { Num=Num*10; Num=*ptr-'0'; ptr++; } if(Neg) Num*=-1; return Num; }
|
|
IP Logged |
|
|
|
ashishjain87
Newbie

 Beware what you wish for ...
Gender: 
Posts: 9
|
 |
Re: ASCII to INT
« Reply #5 on: Jul 7th, 2008, 6:28am » |
Quote Modify
|
A cleaner but similar solution - (for unsigned nos) unsigned int atoi(char *s) { unsigned int sum=0; while(*s != '\0') { if('0' <= *s && *s <= '9') sum=sum*10 + *s-'0'; else return sum; s++; } return sum; } Q. A similar problem (K & R Second Edition) Exercise 2-3. Write a function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F. My solution :- inline unsigned int hexDigit(char c) { return ('0' <= c && c <= '9')*(c-'0') + ('A' <= c && c <= 'F')*(10+c-'A') + ('a' <= c && c <= 'f')*(10+c-'a');/* if c is not valid then value is = 0*/ } unsigned int htoi(char *s) { if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s+=2; unsigned int n=0; while(*s != '\0') { n=16*n + hexDigit(*s); s++; } /* 1fr0 will be considered as 1f00 */ return n; }
|
|
IP Logged |
|
|
|
|