Re: [Boost-users] lexical_cast and string to wstring!

-----Original Message----- From: Thomas Gulbrandsen [mailto:thomas@tansa.no] Sent: Tuesday, March 21, 2006 11:30 AM To: boost-users@lists.boost.org Subject: [Boost-users] lexical_cast and string to wstring!
Hello!
I have to convert a std::wstring to a std::string and tried the following small test program.
But this will not compile on MSVC8.0 or MSVC7.1. Is the boost::lexical_cast suppost to handle this conversion?
As far I know, it's not (corrections, anybody?). My usual method to perform this conversion on Windows is to use WideCharToMultiByte, found in the Windows Platform SDK. Note, however, this function is designed to work with C-style strings and/or fixed-width. You might consider a function similar to the following (warning: untested code): #include <string> #include <windows.h> std::string NarrowString (const std::wstring &src) { //A temporary buffer to store the converted string. //It must be large enough to store the string. char buffer[100]; //Convert the string to UTF-8 and store it in the local buffer. //If you're looking for your native encoding, replace //CP_UTF8 with CP_ACP and look into the dwFlags, lpDefaultChar, and //lpUsedDefaultChar parameters to WideCharToMultiByte. int narrowWidth = WideCharToMultiByte (CP_UTF8, 0, src.data(), src.length(), buffer, sizeof buffer, NULL, NULL); //Convert the buffer to a string and return it return std::string (buffer, narrowWidth); } Other platforms have a similar facility. If you're looking for a cross-platform solution, I would recommend the ICU library. Also, as I understand it, ICU is the "similar facility" for several variants of Unix.

Thank you, Andrew! I will check out the ICU Library. Have a nice day :) -Thomas -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org]On Behalf Of Andrew Holden Sent: Tuesday, March 21, 2006 5:59 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] lexical_cast and string to wstring!
-----Original Message----- From: Thomas Gulbrandsen [mailto:thomas@tansa.no] Sent: Tuesday, March 21, 2006 11:30 AM To: boost-users@lists.boost.org Subject: [Boost-users] lexical_cast and string to wstring!
Hello!
I have to convert a std::wstring to a std::string and tried the following small test program.
But this will not compile on MSVC8.0 or MSVC7.1. Is the boost::lexical_cast suppost to handle this conversion?
As far I know, it's not (corrections, anybody?). My usual method to perform this conversion on Windows is to use WideCharToMultiByte, found in the Windows Platform SDK. Note, however, this function is designed to work with C-style strings and/or fixed-width. You might consider a function similar to the following (warning: untested code): #include <string> #include <windows.h> std::string NarrowString (const std::wstring &src) { //A temporary buffer to store the converted string. //It must be large enough to store the string. char buffer[100]; //Convert the string to UTF-8 and store it in the local buffer. //If you're looking for your native encoding, replace //CP_UTF8 with CP_ACP and look into the dwFlags, lpDefaultChar, and //lpUsedDefaultChar parameters to WideCharToMultiByte. int narrowWidth = WideCharToMultiByte (CP_UTF8, 0, src.data(), src.length(), buffer, sizeof buffer, NULL, NULL); //Convert the buffer to a string and return it return std::string (buffer, narrowWidth); } Other platforms have a similar facility. If you're looking for a cross-platform solution, I would recommend the ICU library. Also, as I understand it, ICU is the "similar facility" for several variants of Unix. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Andrew Holden
-
Thomas Gulbrandsen