Sunday, December 6, 2009

String conversion functions

Three functions take a string as an argument and convert it to a number of the type shown below:
#include <stdlib.h>
double atof(const char *nptr);
long atol(const char *nptr);
int atoi(const char *nptr);
For each of the functions, the number is converted and the result returned. None of them guarantees to set errno (although they may do in some implementations), and the results of a conversion which overflows or cannot be represented is undefined.
Most of these are fairly straightforward to use. For example:
char *str1 = "100";
char *str2 = "55.444";
char *str3 = " 1234";
char *str4 = "123four";
char *str5 = "invalid123";

int i;
float f;

i = atoi(str1); /* i = 100 */
f = atof(str2); /* f = 55.44 */
i = atoi(str3); /* i = 1234 */
i = atoi(str4); /* i = 123 */
i = atoi(str5); /* i = 0 */
  • Leading blank characters are skipped.
  • Trailing illegal characters are ignored.
  • If conversion cannot be made zero is returned and errno  is set with the value ERANGE.  
More sophisticated functions are:
#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
long strtol(const char *nptr, char **endptr, int base);
unsigned long strtoul(const char *nptr,char **endptr, int base);
All three functions work in a similar way. Leading white space is skipped, then a subject sequence, resembling an appropriate constant, is found, followed by a sequence of unrecognized characters. The trailing null at the end of a string is always unrecognized. The subject sequence can be empty. The subject sequences are determined as follows:
Optional + or -, followed by a digit sequence containing an optional decimal point character, followed by an optional exponent. No floating suffix will be recognized. If there is no decimal point present, it is assumed to follow the digit sequence.
Optional + or -, followed by a digit sequence. The digits are taken from the decimal digits or an upper or lower case letter in the range a–z of the English alphabet; the letters are given the values 10–35 respectively. The base argument determines which values are permitted, and may be zero, or otherwise 2–36. Only ‘digits’ with a value less than that of base are recognized. A base of 16 permits the characters 0x or 0X to follow the optional sign. A base of zero permits the input of characters in the form of a C integer constant. No integer suffix will be recognized.
Identical to strtol but with no sign permitted.
If endptr is non-null, the address of the first unrecognized character is stored in the object that it points to. If the subject sequence is empty or has the wrong form, this is the value of nptr.
If a conversion can be performed, the functions convert the number and return its value, taking into account a leading sign where permitted. Otherwise they return zero. On overflow or error the action is as follows:
On overflow, returns ±HUGE_VAL according to the sign of the result; on underflow, returns zero. In either case, errno is set to ERANGE.
On overflow, LONG_MAX or LONG_MIN is returned according to the sign of the result, errno is set to ERANGE.
On overflow, ULONG_MAX is returned, errno is set to ERANGE.
If the locale is not the "C" locale, there may be other subject sequences recognised depending on the implementation.

No comments:

Post a Comment