How to convert wstring into string?

前端 未结 17 2158
北海茫月
北海茫月 2020-11-22 13:10

The question is how to convert wstring to string?

I have next example :

#include 
#include 

int main()
{
    std::wstr         


        
17条回答
  •  -上瘾入骨i
    2020-11-22 13:13

    In my case, I have to use multibyte character (MBCS), and I want to use std::string and std::wstring. And can't use c++11. So I use mbstowcs and wcstombs.

    I make same function with using new, delete [], but it is slower then this.

    This can help How to: Convert Between Various String Types

    EDIT

    However, in case of converting to wstring and source string is no alphabet and multi byte string, it's not working. So I change wcstombs to WideCharToMultiByte.

    #include 
    
    std::wstring get_wstr_from_sz(const char* psz)
    {
        //I think it's enough to my case
        wchar_t buf[0x400];
        wchar_t *pbuf = buf;
        size_t len = strlen(psz) + 1;
    
        if (len >= sizeof(buf) / sizeof(wchar_t))
        {
            pbuf = L"error";
        }
        else
        {
            size_t converted;
            mbstowcs_s(&converted, buf, psz, _TRUNCATE);
        }
    
        return std::wstring(pbuf);
    }
    
    std::string get_string_from_wsz(const wchar_t* pwsz)
    {
        char buf[0x400];
        char *pbuf = buf;
        size_t len = wcslen(pwsz)*2 + 1;
    
        if (len >= sizeof(buf))
        {
            pbuf = "error";
        }
        else
        {
            size_t converted;
            wcstombs_s(&converted, buf, pwsz, _TRUNCATE);
        }
    
        return std::string(pbuf);
    }
    

    EDIT to use 'MultiByteToWideChar' instead of 'wcstombs'

    #include 
    #include 
    #include "string_util.h"
    
    std::wstring get_wstring_from_sz(const char* psz)
    {
        int res;
        wchar_t buf[0x400];
        wchar_t *pbuf = buf;
        boost::shared_ptr shared_pbuf;
    
        res = MultiByteToWideChar(CP_ACP, 0, psz, -1, buf, sizeof(buf)/sizeof(wchar_t));
    
        if (0 == res && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            res = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
    
            shared_pbuf = boost::shared_ptr(new wchar_t[res]);
    
            pbuf = shared_pbuf.get();
    
            res = MultiByteToWideChar(CP_ACP, 0, psz, -1, pbuf, res);
        }
        else if (0 == res)
        {
            pbuf = L"error";
        }
    
        return std::wstring(pbuf);
    }
    
    std::string get_string_from_wcs(const wchar_t* pcs)
    {
        int res;
        char buf[0x400];
        char* pbuf = buf;
        boost::shared_ptr shared_pbuf;
    
        res = WideCharToMultiByte(CP_ACP, 0, pcs, -1, buf, sizeof(buf), NULL, NULL);
    
        if (0 == res && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            res = WideCharToMultiByte(CP_ACP, 0, pcs, -1, NULL, 0, NULL, NULL);
    
            shared_pbuf = boost::shared_ptr(new char[res]);
    
            pbuf = shared_pbuf.get();
    
            res = WideCharToMultiByte(CP_ACP, 0, pcs, -1, pbuf, res, NULL, NULL);
        }
        else if (0 == res)
        {
            pbuf = "error";
        }
    
        return std::string(pbuf);
    }
    

提交回复
热议问题