如何将char转换为Platform::String^
?如何从char转换为Platform :: String ^(C++/CLI)
Platform::String
的文档在这里,但没有提及如何转换为和来自不同的数据类型。
http://msdn.microsoft.com/en-us/library/windows/apps/hh755812(v=vs.110).aspx
如何将char转换为Platform::String^
?如何从char转换为Platform :: String ^(C++/CLI)
Platform::String
的文档在这里,但没有提及如何转换为和来自不同的数据类型。
http://msdn.microsoft.com/en-us/library/windows/apps/hh755812(v=vs.110).aspx
ref new Platform::String(&ch, 1);
使用适当的构造函数工作:
// this is a pointer to the start of an array of char16
char16* c;
// this is the number of chars in the array
// (not including the null char if the array is null-terminated)
int n;
Platform::String^ str(c, n);
如果你的 “的char16s阵列” 是空值终止的,你也可以使用这样的:
Platform::String^ str(c);
我发现了一个方法convert char[]
到Platform::String
。
char char_str[] = "Char string";
std::string s_str = std::string(char_str);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
Platform::String^ p_string = ref new Platform::String(w_char);
我希望有比我的方法更有效的方法。
String^ StringFromAscIIChars(char* chars)
{
size_t newsize = strlen(chars) + 1;
wchar_t * wcstring = new wchar_t[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, chars, _TRUNCATE);
String^ str=ref new Platform::String(wcstring);
delete[] wcstring;
return str;
}
也看到这个MSDN链接:http://msdn.microsoft.com/en-us/library/ms235631.aspx
尝试类似的东西:
#include <cvt/wstring>
#include <codecvt>
...
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
std::wstring wide_string = convert.from_bytes(char_ptr);
Platform::String^ platform_string = ref new Platform::String(wide_string.c_str());
谢谢,但这也行不通,'gcnew is undefined' – joe 2012-07-18 16:46:32
@joe检查当前变种 – 2012-07-18 16:49:40