Built-in to wide string conversion

S

Saeed Amrollahi

Dear All
Hi

I usually use the following function for built-in types to std::string
conversion:
// convert a type (typically built-in type) to std::string
template<class T>
inline std::string ToStdStr(const T& t)
{
std::istringstream s;
s << t;
return s.str();
}

What is the equivalent function for std::wstring conversion?
Unfortunately the following code doesn't work:
// convert a type (typically built-in type) to std::wstring
template<class T>
inline std::wstring ToWideStdStr(const T& t)
{
std::wistringstream s;
s << t;
return s.str();
}

In other words, I am looking for the << operator for wstring. Does it
relate to locale?

Thanks in advance.

Regards,
Saeed Amrollahi
 
S

Saeed Amrollahi

* Saeed Amrollahi:



An istringstream is the wrong kind of stream.

Use an ostringstream.

Or better, use boost::lexical_cast instead of building your own.








#include <iostream>     // std::cout, std::eek:stream
#include <ostream>      // operator<<, std::endl
#include <sstream>      // std::eek:stringstream

template<class T>
inline std::wstring wideString(const T& t)
{
     std::wostringstream s;
     s << t;
     return s.str();

}

int main()
{
     using namespace std;

     wcout << wideString( 123 ) << endl;

}

Note that MingW g++ (Windows version of g++) lacks support for wide streams.


Yes.  But it's a mess, so I'm no longer familiar with it.  Hopefully if you have
any concrete questions in that direction, others can answer them.

Cheers, & hth.,

- Alf- Hide quoted text -

- Show quoted text -

Dear Alf
Hi

As before your answer is good and resolved my problem. Of course I
made mistake in
using ostrstream/wostrstream.

thank you
Saeed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top