|
Boost Users : |
From: Thomas Gulbrandsen (thomas_at_[hidden])
Date: 2006-03-21 12:17:05
Thank you, Andrew!
I will check out the ICU Library.
Have a nice day :)
-Thomas
-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]]On Behalf Of Andrew Holden
Sent: Tuesday, March 21, 2006 5:59 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] lexical_cast and string to wstring!
> -----Original Message-----
> From: Thomas Gulbrandsen [mailto:thomas_at_[hidden]]
> Sent: Tuesday, March 21, 2006 11:30 AM
> To: boost-users_at_[hidden]
> 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_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net