You Are Here:

Community: Wiki

This page was last modified on 14 October 2008, at 12:15.

CS001100 - Converting strings and numbers using string streams

From Forum Nokia Wiki



ID CS001100 Creation date September 16, 2008
Platform S60 3rd Edition, FP2 Tested on devices Nokia 6220 Classic
Category Open C/C++ Subcategory Files/Data


Keywords (APIs, classes, methods, functions): istringstream, ostringstream, stringstream, stringstream::str(), stringstream::fail()

Overview

This code snippet shows how to use string streams to convert strings to numbers and vice versa. The sstream library provides a functionality similar to sscanf() and sprintf() in the standard C library. The three main classes of this library are istringstream, ostringstream, and stringstream. An ostringstream object can be used to write to a string, an istringstream object can be used to read from a string, and a stringstream object can be used for both input and output to a string. String streams provide a great help when conversions between strings and numbers are needed.

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

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <iostream>  
#include <sstream>
#include <string>
 
using namespace std;
 
//template conversion functions that can be used
//with different number types
 
template<typename T>
T ConvertStringToNumber(const string& str)
{
istringstream ss(str);
T number = 0;
 
ss >> number;
 
if (ss.fail( ))
{
throw invalid_argument("ConvertStringToNumber:" + str);
}
 
return number;
}
 
template<typename T>
string ConvertNumberToString (const T number)
{
ostringstream ss;
 
ss << number;
 
return ss.str();
}
 
 
int main()
{
// -- converting with stringstream --
int m = 1;
int n = 0;
string s1 = "";
string s2 = "123";
 
stringstream ss;
 
//number to string
ss << m;
s1 = ss.str();
cout << "s1:" << s1 << endl; //s1:1
 
//clear/reuse stringstream object
ss.str("");
 
//string to number
ss << s2;
ss >> n;
cout << "n:" << n << endl; //n:123
 
// -- converting with istringstream and ostringstream --
try
{
string intString = ConvertNumberToString<int>(1234);
cout << intString << endl; //1234
 
string floatString = ConvertNumberToString<float>(1.2);
cout << floatString << endl; //1.2
 
float floatVar = ConvertStringToNumber<float>(floatString);
cout << floatVar << endl; //1.2
 
int intVar = ConvertStringToNumber<int>(intString);
cout << intVar << endl; //1234
 
string exceptionString = "xyz";
intVar = ConvertStringToNumber<int>(exceptionString);
 
//this line is never executed because of exception
cout << intVar << endl;
}
catch (const exception& e)
{
cout << e.what() << endl; //ConvertStringToNumber:xyz
}
 
return 0;
}

Note: There is a known bug in the current Open C++ plug-in that prevents this code snippet to compile when working with the gcce compiler. To fix this problem, open the file stdapis/stlport/stl/_sstream.c and find and replace the following line:

if((_M_str.size() - _M_str._M_stream_pos) >= __n)

with

if((_M_str.size() - _M_str._M_stream_pos) >= (size_t)__n)

Postconditions

Different string-to-number and number-to-string conversions have been executed and are displayed as standard output. Exceptions have been caught because of an invalid conversion argument.

See also

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: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHttpE3aE2fE2f217E2e218E2e225E2e2E3a2082E2findeE78E2ehtmlE253FX 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