You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 17:08.

CS000897 - Converting numbers to C strings

From Forum Nokia Wiki



ID CS000897 Creation date April 17, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N93
Category Open C Subcategory Files/Data


Keywords (APIs, classes, methods, functions): sprintf(), snprintf()

Overview

sprintf() and snprintf() functions can be used in Open C to convert numeric values into formatted numeral strings. Both of these functions return the number of characters printed.

  • sprintf() takes three parameters. The first holds the converted number, the second parameter is a string containing a format specifier, and the third is the number the user wants to convert into a string.
  • snprintf() is a safer version of sprintf() because there is also a fourth size parameter. snprintf() writes at most the given size-1 of the characters printed into the output string.


Note: In order to use this code, you need to install Open C plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  libc.lib

Source file

#include <stdio.h> // sprintf(), snprintf()
 
#define MAX_STR_LEN 8
 
int main(void)
{
int printed = 0;
char numeral_string[MAX_STR_LEN];
 
/* - sprintf() - */
printed = sprintf(numeral_string, "%d", 123);
printf("123 in decimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
 
printed = sprintf(numeral_string, "%x", 123);
printf("123 in hexadecimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
 
/* - snprintf() - */
printed = snprintf(numeral_string, 7, "%o", 123);
printf("123 in octal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
 
/* only first three characters + trailing \0 is written to the buffer */
printed = snprintf(numeral_string, 4, "%d", 1234567890);
printf("123|4567890 in decimal is %s\n", numeral_string);
printf("printed characters: %d\n", printed);
 
return 0;
}

Postconditions

Four different number to string conversions are executed and displayed to the standard output.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fFileE3aMicrokernelE5fArchitectureE2eGIFX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ