please help.
i need to convert a string (with a numeric value) to an integer. how do i do that in c?
thanks a lot.
Printable View
please help.
i need to convert a string (with a numeric value) to an integer. how do i do that in c?
thanks a lot.
Code:int i;
char * string = "1024";
i = atoi(string);
printf("%d", i);
But how come when I try to do this
char *string="ARE9";
int key=atoi(string);
printf("%d", key);
cout<<endl;
it only gives me a 0?
Are you trying to create a hash? I don't have much experience with this, but try playing around with bit ops etc..
here's an example (probably not good, just off the top of my head..)
Give it a try, play around with it. Do a google search.Code:int examplehash(char *string)
{
int i = 0, hash = 0;
while(string[i])
{
hash += string[i];
hash ^= string[i];
++i;
};
return hash;
};
*blah, fixed code tag + missing increment :(
Hi wtyyip!
What did you expect :confused: ?
... so you should use strtol.Code:DESCRIPTION
The atoi() function converts the initial portion of the
string pointed to by nptr to int. The behaviour is the
same as
strtol(nptr, (char **)NULL, 10);
except that atoi() does not detect errors.
In fact... you C library is friendly enough to give you 0, it could return you any value it likes (at least with the ISO90 standard).
alex
I guess my question now is: how to I convert character such as ABC back to their ASCII number.
0=48, A=65, B=66 etc...
int x = 'a';
Pretty simple stuff.
printf( "a is %d", 'a' );
Quzah.