2012-07-18 17 views

Odpowiedz

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

Dzięki, ale to też nie działa, 'gcnew is undefined' – joe

+1

@joe sprawdź aktualny wariant –

1

Użyj odpowiedniego konstruktora do pracy:

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

Jeśli "tablica char16s" jest zakończony zerem, można również korzystać z tego:

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

Kopiowanie dokładnie to, co masz do mojego kodu daje ten błąd C3149: 'platforma :: string': nie można użyj tego typu tutaj bez najwyższego poziomu '^' – joe

+0

@joe Podobno musisz zmienić String na String ^, ale nie pytaj mnie dlaczego. Nigdy nie dotknę dialektu CLI, nawet z 10-metrowym biegunem. – Gigi

8

Znalazłem metodę, która convert char[] do 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); 

Mam nadzieję, że istnieje bardziej efektywny sposób niż moja metoda.

+0

Właśnie tego szukałem - wielkie dzięki. – 3yakuya

+0

Szukałem krótszych metod. Wydaje się, że to jest to. – 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; 
} 

zobaczyć także ten MSDN link: http://msdn.microsoft.com/en-us/library/ms235631.aspx

1

spróbować czegoś takiego:

#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()); 
Powiązane problemy