Discussion:
convert std::string to std::wstring
Hansi
2008-06-18 19:10:07 UTC
Permalink
Hello,

I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got any
results. Can anyone give me an hint what is the best way to do that
(also good performance?)? maybe there is something available to do that?
may be also with some examples.

Thanks
Hansjörg
Tan, Tom (Shanghai)
2008-06-19 03:29:18 UTC
Permalink
Post by Hansi
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got
any
Post by Hansi
results. Can anyone give me an hint what is the best way to do that
(also good performance?)? maybe there is something available to do
that?
Post by Hansi
may be also with some examples.
Use member function assign().
Ex.

// wstring ->string
wstring ws(L"this is a wstring");
string s;
s.assign(ws.begin(), ws.end());// now s = "this is a wstring";

// wstring ->string
string s("this is a wstring");
wstring ws;
ws.assign(s.begin(), s.end());// now ws = L"this is a wstring";
Kamil Zubair
2008-06-19 03:45:39 UTC
Permalink
since char is convertible to wchar_t and vice versa (with a possibility for lost precision of course) I usually just do this:

string s("something");
wstring ws(s.begin(), s.end());

You can do the reverse with wstring to string and as long your wstring doesn't contain non-ASCII characters it should be okay.

--- On Wed, 6/18/08, Hansi <***@web.de> wrote:
From: Hansi <***@web.de>
Subject: [Boost-users] convert std::string to std::wstring
To: boost-***@lists.boost.org
Date: Wednesday, June 18, 2008, 2:10 PM

Hello,

I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got any
results. Can anyone give me an hint what is the best way to do that
(also good performance?)? maybe there is something available to do that?
may be also with some examples.

Thanks
Hansjörg
Robert Ramey
2008-06-19 06:35:13 UTC
Permalink
The serialization library contains both converters in the form of "Dataflow
Iterators"
mb_from_wchar and wchar_from_mb. They are built using generic code of boost
iterators.

Robert Ramey
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got
any results. Can anyone give me an hint what is the best way to do
that (also good performance?)? maybe there is something available to
do that? may be also with some examples.
Thanks
Hansjörg
Max
2008-06-24 00:49:30 UTC
Permalink
Please elaborate. I also have interest.

Thanks
Max
Post by Robert Ramey
The serialization library contains both converters in the form of "Dataflow
Iterators"
mb_from_wchar and wchar_from_mb. They are built using generic code of boost
iterators.
Robert Ramey
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got
any results. Can anyone give me an hint what is the best way to do
that (also good performance?)? maybe there is something available to
do that? may be also with some examples.
Thanks
Hansjörg
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Robert Ramey
2008-06-24 07:40:41 UTC
Permalink
look for wchar_from_mb.hpp and mb_from_wchar.hpp in boost/archive/iterators

for examples look in libs/serialization/test/test_iterators.hpp

Robert Ramey
Post by Max
Please elaborate. I also have interest.
Thanks
Max
Post by Robert Ramey
The serialization library contains both converters in the form of
"Dataflow Iterators"
mb_from_wchar and wchar_from_mb. They are built using generic code
of boost iterators.
Robert Ramey
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to
convert a string to a wstring (and may be vice versa). But til now
I don`t got any results. Can anyone give me an hint what is the
best way to do that (also good performance?)? maybe there is
something available to do that? may be also with some examples.
Thanks
Hansjörg
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Max
2008-06-24 07:37:25 UTC
Permalink
Hello,

Thanks for your information.

Cheers
Max
Post by Robert Ramey
look for wchar_from_mb.hpp and mb_from_wchar.hpp in boost/archive/iterators
for examples look in libs/serialization/test/test_iterators.hpp
Robert Ramey
Post by Max
Please elaborate. I also have interest.
Thanks
Max
Post by Robert Ramey
The serialization library contains both converters in the form of
"Dataflow Iterators"
mb_from_wchar and wchar_from_mb. They are built using generic code
of boost iterators.
Robert Ramey
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to
convert a string to a wstring (and may be vice versa). But til now
I don`t got any results. Can anyone give me an hint what is the
best way to do that (also good performance?)? maybe there is
something available to do that? may be also with some examples.
Thanks
Hansjörg
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boris
2008-06-19 08:44:06 UTC
Permalink
Post by Hansi
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got any
results. Can anyone give me an hint what is the best way to do that
(also good performance?)? maybe there is something available to do that?
may be also with some examples.
See mbstowcs() and wcstombs() in <cstdlib>

Boris
Cory Nelson
2008-06-19 12:18:59 UTC
Permalink
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got any
results. Can anyone give me an hint what is the best way to do that (also
good performance?)? maybe there is something available to do that? may be
also with some examples.
mbstowcs and wcstombs might help, but they can be a crapshoot so make
sure you read their docs before you use them.
--
Cory Nelson
Max
2008-06-24 04:14:55 UTC
Permalink
Post by Cory Nelson
Post by Hansi
Hello,
I have searched a lot in every place what is the best way to convert a
string to a wstring (and may be vice versa). But til now I don`t got any
results. Can anyone give me an hint what is the best way to do that (also
good performance?)? maybe there is something available to do that? may be
also with some examples.
mbstowcs and wcstombs might help, but they can be a crapshoot so make
sure you read their docs before you use them.
Please elaborate.

Thanks
Max
Loading...