2012-07-18 95 views

回答

6
ref new Platform::String(&ch, 1); 
+0

謝謝,但這也行不通,'gcnew is undefined' – joe 2012-07-18 16:46:32

+1

@joe檢查當前變種 – 2012-07-18 16:49:40

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); 
+0

複製正是你必須到我的代碼是什麼賦予了這個錯誤C3149: '平臺::字符串':不能在這裏沒有頂級'^'使用這種類型 – joe 2012-07-18 16:44:44

+0

@joe顯然你必須將String改爲String ^,但不要問我爲什麼。即使是10米的杆子,我也永遠不會碰到CLI方言。 – Gigi 2012-07-18 16:53:39

8

我發現了一個方法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); 

我希望有比我的方法更有效的方法。

+0

正是我在尋找 - 非常感謝。 – 3yakuya 2015-01-13 00:58:46

+0

我在尋找更短的方法。這似乎是它。 – pollaris 2017-05-09 23:27:34

2
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

1

嘗試類似的東西:

#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()); 
相關問題