How would I go about changing a string of numbers like "2546" and return the integer value 2546.
This is what I have so far (this is just my function to convert the string to an integer)
The 48 is in there because the value of 0 is 48, 1 is 49, etc
I realize my "power" will depend on the length of the string of numbers but for now I have it set to 1 so it should work with any 2-digit integers.
Code:
int stringToInt ( char s[] )
{
int i = 0;
int place;
int value = 0;
int power = 1;
for (i=0; s[i]!='\0' ; i++ )
{
place = s[i] - 48;
value = (value + (place*(10^power)));
power = power-1;
}
return place;
}
For some reason I'm only getting the last number as an integer from every string for instance, if the string is 48, I will get 8 returned (again, I'm just testing on 2-digit numbers right now)
I will need this to work with any size string of numbers including negative numbers too.
Any help would be great.
Thanks
Edit: I know I probably should have returned value at the end, but that's still wrong, it gives me a result of 61 when the string is 15, and 52 when the string is 23.