×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Converting C strings to numbers

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Open C/C++]][[Category:Code Snippet]][[Category:Files/Data]][[Category:S60 3rd Edition (initial release)]][[Category:Code Snippet]]
__NOEDITSECTION__
+
{{Archived|timestamp=20120314040752|user=roy.debjit| }}
{{KBCS}}
+
{{ArticleMetaData
+
|id=CS000891
+
|platform=S60 3rd Edition
+
|devices=Nokia N93
+
|category=Open C
+
|subcategory=Files/Data
+
|creationdate=April 9, 2008
+
|keywords=atoi(), atof(), atol(), strtod(), strtol()
+
  
 +
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|devices= Nokia N93
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= S60 3rd Edition
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|author=[[User:Nymanaki]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= atoi(), atof(), atol(), strtod(), strtol()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20080404
 +
|author= [[User:Nymanaki]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= Files/Data
 +
|id= CS000891
 
}}
 
}}
  
 
==Overview==
 
==Overview==
  
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".
+
{{Abstract|The simplest way to convert a string to an integer in Open C is to use {{Icode|atoi()}}. However, {{Icode|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 {{Icode|atoi()}} are {{Icode|atof()}} and {{Icode|atol()}}. 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 the second parameter. The function <tt>strtol()</tt> has also a third parameter to indicate the base of the numeral string.   
+
Functions {{Icode|strtod()}} and {{Icode|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 {{Icode|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 [http://www.developer.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C plug-in].
 
'''Note''': In order to use this code, you need to install [http://www.developer.nokia.com/info/sw.nokia.com/id/91d89929-fb8c-4d66-bea0-227e42df9053/Open_C_SDK_Plug-In.html Open C plug-in].
Line 35: Line 43:
  
 
<code>
 
<code>
LIBRARY libc.lib
+
LIBRARY libc.lib
 
</code>
 
</code>
  
Line 96: Line 104:
 
info
 
info
 
-->
 
-->
 
[[Category:Open_C/C++]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition]][[Category:Code Snippet]]
 

Latest revision as of 21:19, 11 August 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition
S60 3rd Edition (initial release)
Article
Keywords: atoi(), atof(), atol(), strtod(), strtol()
Created: nymanaki (04 Apr 2008)
Last edited: lpvalente (11 Aug 2012)

Contents

[edit] 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.

[edit] MMP file

The following libraries are required:

LIBRARY libc.lib

[edit] 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;
}

[edit] Postconditions

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

This page was last modified on 11 August 2012, at 21:19.
75 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.

×