2012-07-18 9 views

risposta

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

Grazie ma questo non funziona neanche, 'gcnew è indefinito' – joe

+1

@ Joe controllare variante corrente –

1

utilizzare il costruttore appropriato per il lavoro:

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

Se la "serie di char16s" è terminata da null, è anche possibile utilizzare questo:

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

copia esattamente quello che hai nel mio codice dà questo errore C3149: 'Piattaforma :: String': non si può utilizzare questo tipo qui senza un alto livello '^' – joe

+0

@ Joe a quanto pare si deve cambiare String String ^, ma non chiedetemi perché. Non toccherò mai il dialetto CLI, nemmeno con un palo da 10 metri. – Gigi

8

ho trovato un metodo che 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); 

Spero ci sia un modo più efficiente del mio metodo.

+0

Proprio quello che cercavo - grazie mille. – 3yakuya

+0

Ho cercato metodi più corti. Questo sembra essere. – 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; 
} 

vedere anche questo link MSDN: http://msdn.microsoft.com/en-us/library/ms235631.aspx

1

provare qualcosa di simile:

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