如何將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