Originally Posted by
Salem
You should be using strtol(), strtoul() and strtod() for your conversions.
They return better success/fail information.
What do you have to say about this (i think is good):
Code:
//Return's 1 for success, 0 otherwise.
int CString_ToInt(int *dst, char *string){
//Reference where the remaining string will be stored.
char *remaining;
//Just Do It!
*dst = strtol(string, &remaining, 10);
//If there is a remaining string, BAD!
if ( strcmp(remaining, "") != 0 )
{
free(remaining);
return 0;
}
//Free the remaining string.
free(remaining);
//Return Successfully.
return 1;
}
//Return's 1 for success, 0 otherwise.
int CString_ToFloat(float *dst, char *string){
//Reference where the remaining string will be stored.
char *remaining;
//Just Do It!
*dst = (float)strtod(string, &remaining);
//If there is a remaining string, BAD!
if ( strcmp(remaining, "") != 0 )
{
free(remaining);
return 0;
}
//Free the remaining string.
free(remaining);
//Return Successfully.
return 1;
}
//Return's 1 for success, 0 otherwise.
int CString_ToDouble(double *dst, char *string){
//Reference where the remaining string will be stored.
char *remaining;
//Just Do It!
*dst = strtod(string, &remaining);
//If there is a remaining string, BAD!
if ( strcmp(remaining, "") != 0 )
{
free(remaining);
return 0;
}
//Free the remaining string.
free(remaining);
//Return Successfully.
return 1;
}