Saturday, March 17, 2012

Programming - Converting Floats to Strings

Converting Floats to Strings



The sprintf() Function

You should already know that printf() function has vast formatting power built into it. What sprintf() does is let you use that formatting power not for output, but to create a string of text. So instead of displaying the text on the screen, which is what printf() does, sprintf() takes the text and puts it into a string buffer that you create. That's what the S in sprintf() stands for; it's a string-output version of printf(). Here is the format:
sprintf(string,"formatting string",vars);
sprintf() has the same basic format as printf(), but before the formatting string is the name of a character buffer into which the formatted output will be placed, string in the above example.
A typical sprintf() statement may look something like this:
sprintf(whoru,"You are %s.\n",name);
Assume that name is a string variable containing your name. If the contents were, Jacob then the resulting string would be:
You are Jacob.
(Complete with the \n newline). This entire string is then put into the character buffer specified by whoru. (So the whoru buffer contains You are Jacob.\n )
The sprintf() function is prototyped in the STDIO.H header, just like printf().
The following code is for TWOPIE.C, which shows how to save the floating point value of half-a-π to a string variable by using sprintf():

Name: TWOPIE.C

#include <stdio.h>

int main()
{
    float pi = 3.141596;
    char halfpie[80];
    pi/=2;

    sprintf(halfpie, "%f", pi);
    printf("Here is the result: %s\n", halfpie);
    return 0;
}

Type the above source code into your editor. Here's the output:
Here is the result: 1.570798
The sprintf() function works just like printf(), but the string produced is stored in the halfpie buffer. The next line, a regular printf(), prints the string to prove that it's a proper conversion of the floating point value.
Remember, any output from a printf() statement can be put into sprintf() to send the output to a string instead of the display.

Aha! But there is a catch!

The sprintf() function does provide a slight security risk. That's because there is no bounds checking on the buffer. So it's entirely possible that the buffer can overflow, and that's how Bad Things happen.
Instead of using sprintf(), get in the habit of using the snprintf() function. It's essentially the same thing but with an N. That N stands for number, or count, and it limits the amount of information that can be put into the buffer — an important safeguard. Here is the format:
snprintf(string,size,"format string",vars);
The snprintf() function only loads up to size characters into the buffer indicated by string. Any characters beyond that value are ignored. That way you add a safety shutoff valve to the convert-to-string operation.
Here is the second version of the TWOPIE.C program, this one more properly done with the snprintf() function:

Name: TWOPIE1.C

#include <stdio.h>

int main()
{
    float pi = 3.141596;
    char halfpie[80];
    pi/=2;

    snprintf(halfpie, 79, "%f", pi);
    printf("Here is the result: %s\n", halfpie);
    return 0;
}
Yeah, it's the same old program. In fact, only the snprintf() line was changed, first the N was added and then the size of 79 was added. The program's output is the same:
Here is the result: 1.570798
But the program runs a lot more securely because there is no chance of buffer overflow. Remember that!

No comments:

Post a Comment