×
Namespaces

Variants
Actions

Converting strings and numbers using string streams in Open C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: istringstream, ostringstream, stringstream, stringstream::str(), stringstream::fail()
Created: aknyman (03 Sep 2008)
Last edited: hamishwillee (05 Jul 2012)

Contents

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

This page was last modified on 5 July 2012, at 07:13.
270 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.

×