4

Is there a simple way (one line of code would be cool) to convert à std::string to a Platform::String^ in C++/CX ?

I found how to do it the other way(String^ to string) but nothing for this one.

Something like :

std::string str = "Hello World";
Platform::String^ p_str = convertFromString(str);

(In the example it's pointless but when you were working with std::string in c++ and you want to send it to some C# code it makes more sense)

Community
  • 1
  • 1
ashtrail
  • 93
  • 2
  • 9
  • You could always try and use a charArray to convert between them? – Cjen1 Oct 07 '15 at 11:45
  • 3
    Sigh, 22 years of Unicode doesn't stop questions like this. You already know how to convert to std::wstring from that post. Then it is a one-liner, ref new String(wide.c_str()) – Hans Passant Oct 07 '15 at 11:55
  • I could make craft something else (with char * etc...), but I wanted to know if there was a good/simple way of doing it – ashtrail Oct 07 '15 at 11:57

4 Answers4

3

The method works for me

Platform::String ^ convertFromString(const std::string & input)
{
    std::wstring w_str = std::wstring(input.begin(), input.end());
    const wchar_t* w_chars = w_str.c_str();

    return (ref new Platform::String(w_chars));
}
Heefan
  • 385
  • 2
  • 8
  • Worked for me with slight change in return `return (ref new Platform::String(w_chars, w_str.length()));` – miradham Nov 06 '17 at 09:20
2

There is no direct std::string to std::wstring conversion currently possible.

However, we can do a roundabout conversion from std::string to std::wstring, then from std::wstring to Platform::String as follows:

#include <locale>
#include <codecvt>
#include <string>

Platform::String^ stringToPlatformString(std::string inputString) {
  std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  std::wstring intermediateForm = converter.from_bytes(inputString);
  Platform::String^ retVal = ref new Platform::String(intermediateForm.c_str());

  return retVal;
}

See this question for more information on std::string to std::wstring conversion.

Community
  • 1
  • 1
apnorton
  • 2,430
  • 1
  • 19
  • 34
1

If you in the conversion does not like to be limited to Basic Latin (Unicode block), like in my case the need for æøå, this works:

#include <sstream>

Platform::String^ StdStringToPlatformString(std::string str)
{
   std::wstringstream wss;
   wss << str.c_str();
   return ref new Platform::String(wss.str().c_str());
}
Nil0
  • 11
  • 1
0

So basically the answer is no : it's more complicated. You need to first convert the std::string in std::wstring and then use user1's answer (convert std::wstring to Platform::String^). For the string to wstring conversion you can check this other question (which doesn't work for me but whatever, I'll just have to go for a more in depth conversion).

(I had put some code but was told it was terrible to do it like this, since I just do it for debugging and wil delete it anyway I don't care but I don't want to give anti-advice so I deleted that code)

Community
  • 1
  • 1
ashtrail
  • 93
  • 2
  • 9