2012-07-18 20 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()); 
相关问题