Text to Value Conversion

Sure, your programs can accept values from the input stream, but most of the time numeric input comes in the form of text. The issue then becomes how to transform the string “1234” into the value 1234.

The traditional C language function for converting ASCII text into an integer value is atoi(), the ASCII to Integer function. It’s still around, and it requires the stdlib.h header file to be compiler kosher. The problem with atio(), and its ASCII to floating point sister atof(), is that both have been superseded by better functions.

Officially C language programmers don’t use the word “superseded.” They use the term deprecated, which means that a function is still around and it still works, but all the cool kids use a newer, better function. And that word again is deprecated, not depreciated.

For converting text to integers, use the strtol() function, which I read as “string to long.”

A similar function for converting text to floating point values is strtod(), which I would guess is “string to double.”

As with the older functions, strtol() and strtod() require the stdlib.h header. Despite their str prefixes, they are not string.h functions. Remember that.

Also remember that strtol() returns a long int value. So if you’re displaying that value by using the printf() function, use %ld as the conversion character. You can also typecast the value returned by using the (int) or (short) operators if you like.

Here’s some sample code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char input[64];
    long value;

    printf("Enter an integer value: ");
    fgets(input,63,stdin);
    value = strtol(input,NULL,10);
    printf("You typed the value %ld.\n",value);

    return(0);
}

The strtol() function requires the stdlib.h header file. It requries three arguments:

  1. A buffer where the string is stored. The function ignores any initial white space characters, but it also accepts the + or - prefix, and when a hex value is read, the prefix 0x is allowed. The function stops reading the string at the first invalid numeric digit.
  2. The address of a buffer where the first invalid character was read. This item can be set to NULL if you don’t want to store that address. This argument allows you to save that point in the string where the number stops.
  3. The base of the number being read, which would normally be base 10, decimal. Other bases would be 16 or 8 for hexadecimal and octal.

Here is a sample of strtol() where the function’s second argument is used:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char input[64];
    char *street[1];
    long value;

    printf("Enter your street address: ");
    fgets(input,63,stdin);
    value = strtol(input,street,10);
    printf("Your house number is %ld",value);
    printf(" on %s",street[0]);

    return(0);
}

True, you can always read in values with scanf(), especially when only the value itself is input. The strtol() function, however, isn’t as finicky when it comes to sloppy input on behalf of the user.

In the second source code example, some cleanup is necessary: The string stored at street[0] starts with the first non-valid character after the value, which is usually a space for a street address. Also, the string ends with a newline because that’s the way fgets() rolls. And, of course, house numbers are nominal numbers, meaning they’re more names than values, but that’s a minor quibble.

Leave a Reply