How to convert Platform::String to char*?

后端 未结 5 1307
长情又很酷
长情又很酷 2020-12-01 23:53

How do I convert the contents of a Platform::String to be used by functions that expect a char* based string? I\'m assuming WinRT provides helper functions for this but I ju

5条回答
  •  悲哀的现实
    2020-12-02 00:01

    You shouldn't cast a wide character to a char, you will mangle languages using more than one byte per character, e.g. Chinese. Here is the correct method.

    #include 
    #include 
    
    Platform::String^ fooRT = "foo";
    stdext::cvt::wstring_convert> convert;
    std::string stringUtf8 = convert.to_bytes(fooRT->Data());
    const char* rawCstring = stringUtf8.c_str();
    

提交回复
热议问题