Convert const char* to wstring

后端 未结 7 777
难免孤独
难免孤独 2020-12-03 01:24

I\'m working on a native extension for a zinc based flash application and I need to convert a const char* to a wstring.

This is my code:

7条回答
  •  一个人的身影
    2020-12-03 01:38

    On OS X wstring uses UTF-32 rather than UTF-16. You can do the conversion like this:

    #include 
    #include 
    
    // make facets usable by giving them a public destructor
    template 
    class usable_facet
        : public Facet
    {
    public:
        template 
            usable_facet(Args&& ...args)
                : Facet(std::forward(args)...) {}
        ~usable_facet() {}
    };
    
    std::wstring s2ws(std::string const &s) {
        std::wstring_convert<
            usable_facet>
            ,char32_t> convert;
        std::u32string utf32 = convert.from_bytes(s);
        static_assert(sizeof(wchar_t)==sizeof(char32_t),"char32_t and wchar_t must have same size");
        return {begin(utf32),end(utf32)};
    }
    

提交回复
热议问题