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
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.
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.
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.
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.
ref new Platform::String(&ch, 1);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With