2

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.

Cœur
  • 34,719
  • 24
  • 185
  • 251
lulas
  • 804
  • 9
  • 28
  • Possible duplicate of [How do I convert wchar\_t\* to std::string?](https://stackoverflow.com/questions/4339960/how-do-i-convert-wchar-t-to-stdstring) – JHBonarius Apr 05 '18 at 12:05

2 Answers2

4

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!

AngeloDM
  • 387
  • 1
  • 8
2

You can use std::wstring which has a constructor that takes wchar_t*.

bialpio
  • 974
  • 6
  • 17