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
  • 37,241
  • 25
  • 195
  • 267
lulas
  • 860
  • 9
  • 29
  • 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
  • 397
  • 1
  • 8
3

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

bialpio
  • 1,004
  • 7
  • 17