×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Converting C strings to numbers

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
+
{{KBCS}}
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
|-
 
|-
|'''ID''' ||  
+
|'''ID''' || CS000891
|'''Creation date''' || April 4, 2008
+
|'''Creation date''' || April 9, 2008
 
|-
 
|-
|'''Platform''' || S60 3rd Edition, FP1
+
|'''Platform''' || S60 3rd Edition
 
|'''Tested on devices''' || Nokia N93
 
|'''Tested on devices''' || Nokia N93
 
|-
 
|-
Line 22: Line 22:
 
==Overview==
 
==Overview==
  
The simplest way to convert string to integer in OpenC is to use <tt>atoi()</tt>. But <tt>atoi()</tt> does not differentiate between '0' and invalid input and it does not allow to specify which base you are working with. Another analogous functions to <tt>atoi()</tt> are <tt>atof()</tt> and <tt>atol()</tt> and they can be used with variable types double and long.
+
The simplest way to convert a string to an integer in Open C is to use <tt>atoi()</tt>. However, <tt>atoi()</tt> does not differentiate between '0' and invalid input and it does not allow specifying which base you are working with. Other analogous functions to <tt>atoi()</tt> are <tt>atof()</tt> and <tt>atol()</tt>. These functions can be used with the variable types "double" and "long".
  
Functions <tt>strtod()</tt> and <tt>strtol()</tt> offer more control over the conversion process and they will not produce unexpected results on overflow during conversion. These functions make the position of the first unread character in the input string available by assigning it to second parameter. The function <tt>strtol()</tt> has also third parameter to indicate the base of the numeral string.   
+
Functions <tt>strtod()</tt> and <tt>strtol()</tt> offer more control over the conversion process and they will not produce unexpected results on overflow during conversion. These functions make the position of the first unread character in the input string available by assigning it to the second parameter. The function <tt>strtol()</tt> has also a third parameter to indicate the base of the numeral string.   
  
'''Note''': In order to use this code, you need to install [http://www.forum.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C plugin].
+
'''Note''': In order to use this code, you need to install [http://www.forum.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C plug-in].
  
 
This snippet can be self-signed.
 
This snippet can be self-signed.
Line 92: Line 92:
  
 
==Postconditions==
 
==Postconditions==
five different string to number conversions are executed and displayed to standard output.  
+
Five different string-to-number conversions have been executed and are displayed as standard output.  
 
<!-- ==See also==
 
<!-- ==See also==
 
info
 
info
 
-->
 
-->
  
[[Category:Open_C]][[Category:Code Examples]][[Category:Files/Data]]
+
[[Category:Open_C]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition]]

Revision as of 10:08, 9 April 2008


Template:KBCS

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


Keywords (APIs, classes, methods, functions): atoi(), atof(), atol(), strtod(), strtol()

Overview

The simplest way to convert a string to an integer in Open C is to use atoi(). However, atoi() does not differentiate between '0' and invalid input and it does not allow specifying which base you are working with. Other analogous functions to atoi() are atof() and atol(). These functions can be used with the variable types "double" and "long".

Functions strtod() and strtol() offer more control over the conversion process and they will not produce unexpected results on overflow during conversion. These functions make the position of the first unread character in the input string available by assigning it to the second parameter. The function strtol() has also a third parameter to indicate the base of the numeral 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 <stdlib.h> //atoi, atof(), atol(), strtod(), strtol()
#include <stdio.h> //printf
 
int main(void)
{
char* int_str = "123"; /* NOTE: atoi will convert e.g. value "xyz" to '0' */
char* double_str = "123.0";
char* long_str = "123456";
 
int int_result = 0;
double double_result = 0.0;
long long_result = 0;
 
char* string_to_convert = NULL;
char* rest_of_the_string = NULL;
double double_value = 0.0;
long long_value = 0;
 
/* functions atoi(), atof() and atol() */
int_result = atoi(int_str);
printf("The string %s as an integer is = %d\n",int_str,int_result);
 
double_result = atof(double_str);
printf("The string %s as a double is = %f\n",double_str,double_result);
 
long_result = atol(long_str);
printf("The string %s as a long is = %ld\n",long_str,long_result);
 
 
/* functions strtod() and strtol() */
string_to_convert = "123.456RestOfTheString";
double_value = strtod(string_to_convert, &rest_of_the_string);
printf("The string = %s, number = %f, rest = %s\n",
string_to_convert, double_value, rest_of_the_string);
 
string_to_convert = "123456789NoMoreNumbers";
rest_of_the_string = NULL;
long_value = strtol(string_to_convert, &rest_of_the_string, 10 ); /* use base 10 */
printf("The string = %s, number = %ld, rest = %s\n",
string_to_convert, long_value, rest_of_the_string);
 
return 0;
}

Postconditions

Five different string-to-number conversions have been executed and are displayed as standard output.

73 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×