2012-07-18 12 views

Respuesta

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

Gracias pero esto tampoco funciona, 'gcnew no está definido' – joe

+1

@joe verifique la variante actual –

1

utilizar el constructor adecuado para el trabajo:

// 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); 

Si su "serie de char16s" es terminada en nulo, también puede utilizar esto:

Platform::String^ str(c); 
+0

copia exactamente lo que tiene en mi código da este error C3149: 'Plataforma :: string': no ​​se puede use este tipo aquí sin un '^' de alto nivel – joe

+0

@joe Aparentemente tiene que cambiar String a String ^, pero no me pregunte por qué. Nunca tocaré el dialecto CLI, incluso con un poste de 10 metros. – Gigi

8

Encontré un método que convert char[] a 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); 

Espero que haya una manera más eficiente que mi método.

+0

Justo lo que estaba buscando, muchas gracias. – 3yakuya

+0

He buscado métodos más cortos. Esto parece ser eso. – pollaris

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; 
} 

También vea este enlace de MSDN: http://msdn.microsoft.com/en-us/library/ms235631.aspx

1

intentar algo así como que:

#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());