lexical_cast and string to wstring!

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

lexical_cast and string to wstring!

Thomas Gulbrandsen
Hello!

I have to convert a std::wstring to a std::string and tried the following
small test program.

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
int main(int argc, char** argv)
{
    std::wstring ws(L"Hello");
    std::string s(boost::lexical_cast<std::string>(ws);
    std::cout << s << std::endl;
    return 0;
}

But this will not compile on MSVC8.0 or MSVC7.1.
Is the boost::lexical_cast suppost to handle this conversion?

Thanks,
Thomas Gulbrandsen


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: lexical_cast and string to wstring!

Andrew Holden
> -----Original Message-----
> From: Thomas Gulbrandsen [mailto:[hidden email]]
> Sent: Tuesday, March 21, 2006 11:30 AM
> To: [hidden email]
> 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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: lexical_cast and string to wstring!

Thomas Gulbrandsen
Thank you, Andrew!
I will check out the ICU Library.
Have a nice day :)

-Thomas



-----Original Message-----
From: [hidden email]
[mailto:[hidden email]]On Behalf Of Andrew Holden
Sent: Tuesday, March 21, 2006 5:59 PM
To: [hidden email]
Subject: Re: [Boost-users] lexical_cast and string to wstring!


> -----Original Message-----
> From: Thomas Gulbrandsen [mailto:[hidden email]]
> Sent: Tuesday, March 21, 2006 11:30 AM
> To: [hidden email]
> 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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users




_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users