Topic: Converting std::string <--> std::wstring


Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/08/27
Raw View
Richard Brice <BriceR@wsdot.wa.gov> wrote in article <6rvfb7$8e1@news.wsdot.wa.gov>...
> What is the best/easiest way to convert a std::string to a std::wstring and
> back again?

Depends strongly on the interpretation you impose on the std::string contents.
If you believe it's a multibyte string, you can use mbstowcs from the Standard
C library to convert it into a wchar_t buffer. Use wcstombs to go the other way.
If you don't want the mess of determining array lengths, you can use the newer
mbrtowc/wcrtomb functions from Standard C Amendment 1. Or you can try
your hand at calling codecvt<wchar_t, char, mbstate_t> member functions
directly. Easiest of all, if slowest, you can write the string to a file and read it
back with a wide input stream.

OTOH, if you believe that each char element is a lone character, then you
need to do a one-to-one mapping. See btowc/wctob from Standard C/
Amendment 1.

And good luck.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com/hot_news.html



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Richard Brice" <BriceR@wsdot.wa.gov>
Date: 1998/08/26
Raw View
What is the best/easiest way to convert a std::string to a std::wstring and
back again?

Thanks



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]