Search code examples
c++type-conversionwchar-t

Conversion between wchar_t* to string


I was trying to create a client in c++ for a web service using a Service Model Metadata Utility Tool, I have established the communication between the two endpoints, but at the client side I receive a wchar_t*, how can i convert it to a string?

Note: the server side is using encoding of UTF-8.


Solution

  • Use this simple function:

    std::string wchar2string(wchar_t* str)
    {
        std::string mystring;
        while( *str )
          mystring += (char)*str++;
        return  mystring;
    }
    

    I hope that this function can help you!