std::string to std::wstring

Hello, Does Boost contain facilities to convert std::string into std::wstring and the other way around? Thanks in advance

Note that the serialization library includes iterators for just this purpose. See documentation of serialization library: Dataflow iterators. Robert Ramey John Christopher wrote:
Hello, Does Boost contain facilities to convert std::string into std::wstring and the other way around? Thanks in advance

John Christopher wrote:
Hello, Does Boost contain facilities to convert std::string into std::wstring and the other way around?
Should there? std::string a; std::wstring b(a.begin(),a.end()); std::string c(b.begin(),b.end()); All string algorithms in Boost are templates so they should work fine with both std::string and std::wstring. http://www.boost.org/doc/html/string_algo.html Regards, Tobias

On 1/22/07, Tobias Schwinger
John Christopher wrote:
Hello, Does Boost contain facilities to convert std::string into std::wstring and the other way around?
Should there?
std::string a; std::wstring b(a.begin(),a.end()); std::string c(b.begin(),b.end());
All string algorithms in Boost are templates so they should work fine with both std::string and std::wstring.
http://www.boost.org/doc/html/string_algo.html
Regards, Tobias
I think the real question is whether the OP was hoping for Unicode conversions of some kind in the process. ie what 'type' (sematically) are the wchar_t's and char's? UTF16? UTF32? (what size is the wchar_t?) etc. Also, does the last line: std::string c(b.begin(), b.end()) give at least a compiler warning about downconverting from wchar_t to char, and thus losing bits? (Which would be a hint that maybe you need some Unicode conversion or something else.) Tony

Gottlob Frege wrote:
On 1/22/07, *Tobias Schwinger*
mailto:tschwinger@isonews2.com> wrote: John Christopher wrote: > Hello, > Does Boost contain facilities to convert std::string into std::wstring and > the other way around?
Should there?
std::string a; std::wstring b( a.begin(),a.end()); std::string c(b.begin(),b.end());
All string algorithms in Boost are templates so they should work fine with both std::string and std::wstring.
http://www.boost.org/doc/html/string_algo.html
Regards, Tobias
I think the real question is whether the OP was hoping for Unicode conversions of some kind in the process. ie what 'type' (sematically) are the wchar_t's and char's? UTF16? UTF32? (what size is the wchar_t?) etc.
I'm not sure that's what the OP really wanted: If we want to convert back and forth we are probably just using the (compatible) ASCII subset (a typical example: COM calls). A proper conversion to UTF-8 when narrowing the character type can be worse than overflow if the rest of the code can't handle UTF-8. However, Boost.Unicode sure would be a nice addition.
Also, does the last line: std::string c(b.begin(), b.end()) give at least a compiler warning about downconverting from wchar_t to char, and thus losing bits?
Diagnostic messages are implementation-defined. The code I posted does not warn with gcc4 (all warnings enabled), for instance.
(Which would be a hint that maybe you need some Unicode conversion or something else.)
Yeah, like most compiler warnings, it means "know what you're doing" ;-). Regards, Tobias

std::string a; std::wstring b( a.begin(),a.end()); std::string c(b.begin(),b.end());
for a while now, I have started wondering if I was not shooting myself in
Hi, The reason I asked is that while I have been using the foot without knowing... I am no Unicode expert and Boost facilities in that area would certainly give me some confidence. Boost is nice also because codes are visited by skilled and experienced programmers and I am glad to benefit from that... :) More generally, I am still struggling on which string class I should use: std::string, std::wstring, some custom class... Tobias, just like everybody I guess, I do share the idea that a boost::unicode would be a really nice addition.

John Christopher wrote:
Hi,
std::string a; std::wstring b( a.begin(),a.end()); std::string c(b.begin(),b.end());
for a while now, I have started wondering if I was not shooting myself in
The reason I asked is that while I have been using the foot without knowing... I am no Unicode expert and Boost facilities in that area would certainly give me some confidence. Boost is nice also because codes are visited by skilled and experienced programmers and I am glad to benefit from that... :) More generally, I am still struggling on which string class I should use: std::string, std::wstring, some custom class...
Not claiming to be a expert in this field, I typedef std::wstring string; for my project if I need Unicode. I expect the iterators of some Unicode string class to (at least) give me wide characters, regardless of the underlying in-memory encoding. The 'typedef' (placed in some global header) allows me to easily change the string class, if I have to (e.g. for saving memory or bandwidth). std::string is basically just a container for bytes, that is length() will start lying as soon as there are characters that are represented by more than one byte (like with UTF-8). Regards, Tobias

Am Samstag, den 27.01.2007, 16:03 -0500 schrieb John Christopher:
Hi,
std::string a; std::wstring b( a.begin(),a.end()); std::string c(b.begin(),b.end());
for a while now, I have started wondering if I was not shooting myself in
The reason I asked is that while I have been using the foot without knowing... I am no Unicode expert and Boost facilities in that area would certainly give me some confidence. Boost is nice also because codes are visited by skilled and experienced programmers and I am glad to benefit from that... :) More generally, I am still struggling on which string class I should use: std::string, std::wstring, some custom class... Tobias, just like everybody I guess, I do share the idea that a boost::unicode would be a really nice addition.
Hello Here is a recent post at boost-devel about unicode support http://thread.gmane.org/gmane.comp.lib.boost.devel/151673/focus=151686 The related library: http://utfcpp.sourceforge.net/ I never used it, but it looks good to me. HTH Maik
participants (5)
-
Gottlob Frege
-
John Christopher
-
Maik Beckmann
-
Robert Ramey
-
Tobias Schwinger