2015-10-07 144 views
0

是否有一种简单的方法(一行代码很酷)将àstd :: string转换为C++/CX中的Platform :: String^?C++/CX:将std :: string转换为Platform :: String^

我找到了how to do it the other way(字符串^到字符串),但没有为这一个。

喜欢的东西:

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

(在这是毫无意义的,但比如当你用的std ::在C++字符串的情况下想将它发送到一些C#代码,它更有意义)

+0

你总是可以尝试使用一个charArray它们之间的转换? – Cjen1

+2

感叹,22年的Unicode并不能阻止这样的问题。您已经知道如何从该帖子转换为std :: wstring。然后它是一个单线程,ref新的字符串(wide.c_str()) –

+0

我可以做其他工具(用char *等等......),但我想知道是否有一个好/简单的方法它 – ashtrail

回答

-1

所以基本上答案是否定的:它更复杂。你需要先在std :: wstring中转换std :: string,然后使用user1的回答(将std :: wstring转换为Platform :: String ^)。 对于字符串到wstring的转换,你可以检查this other question(这不适用于我,但无论如何,我只需要进行更深入的转换)。

(我已经放了一些代码,但被告知这样做很糟糕,因为我只是为了调试而做,而且无论如何都将其删除,我不在乎,但我不想提供反建议所以我删除了该代码)

+0

好^^如果你这么说 – ashtrail

0

目前没有直接std::stringstd::wstring的转换可能。

然而,我们可以做一个迂回的转换,从std::stringstd::wstring,然后从std::wstringPlatform::String如下:

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

std::stringstd::wstring转换的更多信息,请参见this question

0

的方法对我的作品

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)); 
} 
+0

工作对我来说略有改变返回'返回(ref new Platform :: String(w_chars,w_str.length()));' – miradham