HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcppModerateCanonical

How to convert wstring into string?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howconvertstringwstringinto

Problem

The question is how to convert wstring to string?

I have next example :

#include 
#include 

int main()
{
    std::wstring ws = L"Hello";
    std::string s( ws.begin(), ws.end() );

  //std::cout <<"std::string =     "<<s<<std::endl;
    std::wcout<<"std::wstring =    "<<ws<<std::endl;
    std::cout <<"std::string =     "<<s<<std::endl;
}


the output with commented out line is :

std::string =     Hello
std::wstring =    Hello
std::string =     Hello


but without is only :

std::wstring =    Hello


Is anything wrong in the example? Can I do the conversion like above?

EDIT

New example (taking into account some answers) is

#include 
#include 
#include 
#include 

int main()
{
    setlocale(LC_CTYPE, "");

    const std::wstring ws = L"Hello";
    const std::string s( ws.begin(), ws.end() );

    std::cout<<"std::string =     "<<s<<std::endl;
    std::wcout<<"std::wstring =    "<<ws<<std::endl;

    std::stringstream ss;
    ss << ws.c_str();
    std::cout<<"std::stringstream =     "<<ss.str()<<std::endl;
}


The output is :

std::string =     Hello
std::wstring =    Hello
std::stringstream =     0x860283c


therefore the stringstream can not be used to convert wstring into string.

Solution

Here is a worked-out solution based on the other suggestions:

#include 
#include 
#include 
#include 
#include 

int main() {
  std::setlocale(LC_ALL, "");
  const std::wstring ws = L"ħëłlö";
  const std::locale locale("");
  typedef std::codecvt converter_type;
  const converter_type& converter = std::use_facet(locale);
  std::vector to(ws.length() * converter.max_length());
  std::mbstate_t state;
  const wchar_t* from_next;
  char* to_next;
  const converter_type::result result = converter.out(state, ws.data(), ws.data() + ws.length(), from_next, &to[0], &to[0] + to.size(), to_next);
  if (result == converter_type::ok or result == converter_type::noconv) {
    const std::string s(&to[0], to_next);
    std::cout <<"std::string =     "<<s<<std::endl;
  }
}


This will usually work for Linux, but will create problems on Windows.

Code Snippets

#include <string>
#include <iostream>
#include <clocale>
#include <locale>
#include <vector>

int main() {
  std::setlocale(LC_ALL, "");
  const std::wstring ws = L"ħëłlö";
  const std::locale locale("");
  typedef std::codecvt<wchar_t, char, std::mbstate_t> converter_type;
  const converter_type& converter = std::use_facet<converter_type>(locale);
  std::vector<char> to(ws.length() * converter.max_length());
  std::mbstate_t state;
  const wchar_t* from_next;
  char* to_next;
  const converter_type::result result = converter.out(state, ws.data(), ws.data() + ws.length(), from_next, &to[0], &to[0] + to.size(), to_next);
  if (result == converter_type::ok or result == converter_type::noconv) {
    const std::string s(&to[0], to_next);
    std::cout <<"std::string =     "<<s<<std::endl;
  }
}

Context

Stack Overflow Q#4804298, score: 38

Revisions (0)

No revisions yet.