Thursday, 5 September 2013

Converting std::wsting to char* with wcstombs_s

Converting std::wsting to char* with wcstombs_s

I have input strings that contain only digits (just the plain Latin ones,
0-9, so for example "0123"), stored as std::wstring, and I need each as a
char*. What's the best way for me to do this? This is my initial approach:
void type::convertWStringToCharPtr(_In_ std::wstring input, _Out_ char *
outputString)
{
size_t outputSize = input.length() + 1; // +1 for null terminator
outputString = new char[outputSize];
size_t charsConverted = 0;
const wchar_t * inputW = input.c_str();
wcstombs_s(&charsConverted, outputString, outputSize, inputW,
input.length());
// TODO verify charsConverted = outputSize
}
However this throws an exception in the call to wcstombs_s (though I can't
determine what - just "Project has triggered a breakpoint.")

No comments:

Post a Comment