甲wchar_t串是由16位为单位,一个LPSTR是指向八位字节的字符串,象这样定义的:
typedef char* PSTR, *LPSTR;
Run Code Online (Sandbox Code Playgroud)
重要的是LPSTR 可以以空值终止.
当从翻译wchar_t到LPSTR,你必须在编码使用的决定.完成后,您可以使用该WideCharToMultiByte功能执行转换.
例如,以下是如何将宽字符串转换为UTF8,使用STL字符串来简化内存管理:
#include <windows.h>
#include <string>
#include <vector>
static string utf16ToUTF8( const wstring &s )
{
const int size = ::WideCharToMultiByte( CP_UTF8, 0, s.c_str(), -1, NULL, 0, 0, NULL );
vector<char> buf( size );
::WideCharToMultiByte( CP_UTF8, 0, s.c_str(), -1, &buf[0], size, 0, NULL );
return string( &buf[0] );
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此函数将a转换wchar_t*为LPSTR如下所示:
const wchar_t *str = L"Hello, World!";
std::string utf8String = utf16ToUTF8( str );
LPSTR lpStr = utf8String.c_str();
Run Code Online (Sandbox Code Playgroud)