本文介绍了如何将PCWSTR转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜。我想将类型PCWSTR转换为char * .tnx





hi. i want to convert type PCWSTR to char *.tnx


HWND hwnd;
CComHeapPtr spszLocation;
if (FAILED(GetBrowserInfo(svar.pdispVal, &hwnd,&spszLocation))) continue;
LPCWSTR tmp=static_cast(spszLocation);
size_t wcsChars = wcslen( tmp);
MD5 md5MdFive;
unsigned char ucMdfiveExit[16];
md5MdFive.ConvertMD5(spszLocation,(unsigned long)iDataSize,ucMdfiveExit);
std::wcout << hwnd<< L" "<< static_cast<PCWSTR>(spszLocation)<< std::endl;







是const unsigned char *但是我有LPCWSTR





in the code above the first parameter of the convertmd5 is const unsigned char * but i have LPCWSTR

推荐答案


wstring wstr( L"abcdef" );
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';





或特定于您的情况:





or to be specific to your case:

PCWSTR pstr(L"abcdef");
int len = WideCharToMultiByte(CP_ACP, 0, pstr, wcslen(pstr), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, pstr, wcslen(pstr), buffer, len, NULL, NULL);
buffer[len] = '\0';



这篇关于如何将PCWSTR转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:42