Alessandro
Alessandro

Reputation: 886

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.

Upvotes: 2

Views: 5013

Answers (2)

AngeloDM
AngeloDM

Reputation: 417

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!

Upvotes: 4

bialpio
bialpio

Reputation: 1024

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

Upvotes: 3

Related Questions