Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from char to Platform::String^ (C++/CLI)

How do you convert from a char to a Platform::String^?

The documentation for Platform::String is here but does not mention how to convert to and from different data types.

http://msdn.microsoft.com/en-us/library/windows/apps/hh755812(v=vs.110).aspx

like image 680
joe Avatar asked Jul 18 '12 16:07

joe


People also ask

Can char array be convert to string C++?

3. C++ String inbuilt constructor. In the context of conversion of char array to string, we can use C++ String Constructor for the same. This constructor takes a sequence of characters terminated by a null character as an input parameter.

How do you convert Lpcwstr to string?

This LPCWSTR is Microsoft defined. So to use them we have to include Windows. h header file into our program. To convert std::wstring to wide character array type string, we can use the function called c_str() to make it C like string and point to wide character string.

What is a Wstring C++?

This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function.


3 Answers

I found a method to convert char[] to 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);

I hope there is a more efficient way than my method.

like image 61
xiaoke Avatar answered Nov 02 '22 04:11

xiaoke


ref new Platform::String(&ch, 1);
like image 22
Serj-Tm Avatar answered Nov 02 '22 06:11

Serj-Tm


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

Also see this MSDN link: http://msdn.microsoft.com/en-us/library/ms235631.aspx

like image 41
A.J.Bauer Avatar answered Nov 02 '22 06:11

A.J.Bauer