Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
This page was last modified 12:15, 14 October 2008.

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 Discussions
Thread Thread Starter Forum Replies Last Post
problem about Reading data from a RESOURCE FILE to an application windalfred0 General Symbian C++ 4 2005-12-19 16:59
What are valid PSM numbers yanfu_2000 Bluetooth Technology 0 2003-08-15 09:58
Beginner :- Sample applications? parimalgupta General Symbian C++ 2 2003-10-06 21:42
The Garbage Collection not working on S60 Fp1 devices ravibabu Mobile Java General 23 2008-06-06 15:49
how to send 8-bit sms xlisec Python 5 2008-04-18 05:23
 
Powered by MediaWiki
RDF Facets: qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX