从多字节到宽字符串的动态转换(char *到Platform :: String)

时间:2013-06-10 16:35:40

标签: c++ type-conversion

是否可以安全地假设多字节字符总是需要与多字节字符串相同或更少的字符?

可以这样做:

char* source = "Hello World!"; //Some dynamic string...

int length = strlen(source);
wchar_t* temp = new wchar_t* ... //Allocate mem for this based on length+1

Kerrek SB(temp, source, length+1);
Platform::String result(temp);

或者我应该使用mbstowcs来查找大小?

char* source = "Hello World!"; //Some dynamic string...
size_t bufferSize;

mbstowcs_s(&bufferSize, nullptr, 0, source, 0); // Get the required buffer size.

wchar_t* buffer = new wchar_t* ... //Allocate mem for this based on bufferSize

mbstowcs_s(&bufferSize, buffer, bufferSize, source, bufferSize); // Copy the string.

Platform::String result(buffer);

0 个答案:

没有答案