C++ convert between string and CString (LPWSTR)

本文介绍如何在C++中实现std::string与LPCWSTR之间的相互转换,包括使用MultiByteToWideChar和wcstombs_s函数的具体步骤。适用于需要处理Unicode与ANSI编码转换的开发场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Summary

Encountered problem in convert from string to CString (LPCWSTR), and the reverse convert, find out the way to convert between these two types and tested in Visual Studio with successful result.

The unicode setting is configured in the Visual Studio project property page –> Configuration Properties –> General –> Character Set –> Use Unicode Character Set, as below picture shows,
VS unicode setting

Different string types description as below, as How to convert std::string to LPCSTR? mentioned,

LPSTR - (long) pointer to string - char *
LPCSTR - (long) pointer to constant string - const char *
LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *
LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t *
LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *

C++ convert from string to LPCWSTR

As you know, std::string is char* type, while LPCWSTR ,LPWSTRor CString is wchar_t* as long as the Visual Studio configured as Unicode Character Set.
I am using How to convert std::string to LPCSTR? solution as below code solved this problem,

LPWSTR ConvertString(const std::string& instr)
{
    // Assumes std::string is encoded in the current Windows ANSI codepage
    int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);

    if (bufferlen == 0)
    {
        // Something went wrong. Perhaps, check GetLastError() and log.
        return 0;
    }

    // Allocate new LPWSTR - must deallocate it later
    LPWSTR widestr = new WCHAR[bufferlen + 1];

    ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);

    // Ensure wide string is null terminated
    widestr[bufferlen] = 0;

    // Do something with widestr
    return widestr;
    //delete[] widestr;
}

Refer to the How to convert string to LPCTSTR? solution 5, it is the similar solution as above by using MultiByteToWideChar function,

std::wstring s2ws(const std::string& s)
{
 int len;
 int slength = (int)s.length() + 1;
 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
 wchar_t* buf = new wchar_t[len];
 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
 std::wstring r(buf);
 delete[] buf;
 return r;
}

std::string s;

#ifdef UNICODE
std::wstring stemp = s2ws(s); // Temporary buffer is required
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = s.c_str();
#endif>

C++ convert from LPCWSTR to string

To convert from LPCWSTR to string, can split into two steps, first step convert fromCString to wchar_t, 2nd step, convert from wchar_t to char*, as below code shows,

    //convert from CString to char* , first from CString to wchar_t then to char *
    wchar_t wCharString = sFile.GetBuffer(sFile.GetLength()+1); //CString to wchar_t
    size_t origsize = wcslen(wCharString) + 1;
    size_t convertedChars = 0;
    char gszFile[100] = {0};
    wcstombs_s(&convertedChars, gszFile, origsize, wCharString , _TRUNCATE); //from wchar_t to char*

Below code from MSDN is working perfectly for the conversion from wchar_t* to char* by using wcstombs_s function,

// crt_wcstombs_s.c
// This example converts a wide character
// string to a multibyte character string.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main( void )
{
    size_t   i;
    char      *pMBBuffer = (char *)malloc( BUFFER_SIZE );
    wchar_t*pWCBuffer = L"Hello, world.";

    printf( "Convert wide-character string:\n" );

    // Conversion
    wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, 
               pWCBuffer, (size_t)BUFFER_SIZE );

    // Output
    printf("   Characters converted: %u\n", i);
    printf("    Multibyte character: %s\n\n",
     pMBBuffer );

    // Free multibyte character buffer
    if (pMBBuffer)
    {
    free(pMBBuffer);
    }
}

Reference

1, MSDN, Converts a sequence of wide characters to a corresponding sequence of multibyte characters
2, How to convert string to LPCTSTR?
3, How to convert std::string to LPCSTR?


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值