-
-
Save siraj/04f1b8e4c01fcac8529bfac7d43f3e26 to your computer and use it in GitHub Desktop.
WinRT string conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::wstring StringConverter::StringToWideString(const std::string& s) { | |
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.length(), NULL, 0); | |
std::wstring ws(L"", len); | |
wchar_t* pWSBuf = const_cast<wchar_t*>(ws.c_str()); | |
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, pWSBuf, len); | |
return ws; | |
} | |
std::string StringConverter::WideStringToString(const std::wstring& ws) { | |
int len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.length(), 0, 0, NULL, NULL); | |
std::string r("", len); | |
char* pRBuf = const_cast<char*>(r.c_str()); | |
WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.length(), pRBuf, len, NULL, NULL); | |
return r; | |
} | |
Platform::String^ StringConverter::LPSTRToPlatformString(const char* lpstr) { | |
if (lpstr == nullptr || strlen(lpstr) == 0) | |
return ref new Platform::String(); | |
int len = MultiByteToWideChar(CP_UTF8, 0, lpstr, strlen(lpstr), NULL, 0); | |
auto ps = ref new Platform::String(L"", len); | |
wchar_t* pPSBuf = const_cast<wchar_t*>(ps->Data()); | |
MultiByteToWideChar(CP_UTF8, 0, lpstr, -1, pPSBuf, len); | |
return ps; | |
} | |
Platform::String^ StringConverter::StringToPlatformString(const std::string& s) { | |
if (s.empty()) | |
return ref new Platform::String(); | |
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.length(), NULL, 0); | |
auto ps = ref new Platform::String(L"", len); | |
wchar_t* pPSBuf = const_cast<wchar_t*>(ps->Data()); | |
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, pPSBuf, len); | |
return ps; | |
} | |
std::string StringConverter::PlatformStringToString(Platform::String^ ps) { | |
int len = WideCharToMultiByte(CP_UTF8, 0, ps->Data(), ps->Length(), 0, 0, NULL, NULL); | |
std::string r("", len); | |
char* pRBuf = const_cast<char*>(r.c_str()); | |
WideCharToMultiByte(CP_UTF8, 0, ps->Data(), ps->Length(), pRBuf, len, NULL, NULL); | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment