473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Built-in to wide string conversion

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
Jun 27 '08 #1
2 4943
* 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;
An istringstream is the wrong kind of stream.

Use an ostringstream.

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

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.
#include <iostream // std::cout, std::ostream
#include <ostream // operator<<, std::endl
#include <sstream // std::ostringstream

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.

Does it relate to locale?
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
Jun 27 '08 #2
On Apr 14, 10:11*am, "Alf P. Steinbach" <al...@start.nowrote:
* 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;

An istringstream is the wrong kind of stream.

Use an ostringstream.

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


* *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.

#include <iostream* * // std::cout, std::ostream
#include <ostream* * *// operator<<, std::endl
#include <sstream* * *// std::ostringstream

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.
Does it relate to locale?

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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
0
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
4
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any...
1
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
0
by: anthony Lichnewsky | last post by:
Hi, I have here a huge bunch of cygwin-built dlls using heavily posix api calls, and I wanted to know how to link them to some VC .NET library wrapper (using microsoft C runtime libraries this...
0
by: yenruyee | last post by:
how do you use ASP.NET or JSP and MSSQL to built a c2c ecommerce to launch, sell, etc. Site contains user registration and is search engine, enable including an advanced search. has functionality...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
3
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.