如何将wstring转换为字符串? [英] How to convert wstring into string?

查看:104
本文介绍了如何将wstring转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是如何将wstring转换为字符串?



我有下面的例子:

  #include< string> 
#include< iostream>

int main()
{
std :: wstring ws = LHello;
std :: string s(w​​s.begin(),ws.end());

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

带注释行的输出是:

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

但不包含:

  std :: wstring = Hello 

示例中有什么问题吗?



新示例(采用)可以执行上面的转换。



<考虑一些答案)

  #include< string> 
#include< iostream>
#include< sstream>
#include< locale>

int main()
{
setlocale(LC_CTYPE,);

const std :: wstring ws = LHello;
const std :: string s(w​​s.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;
}

输出为:

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

因此,stringstream不能用于将wstring转换为字符串。

解决方案

这里是一个基于其他建议的解决方案:

  #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或result == converter_type :: noconv){
const std :: string s(& to [0],to_next);
std :: cout<<std :: string =<< s<< std :: endl;
}
}

这通常适用于Linux,在Windows上。


The question is how to convert wstring to string?

I have next example :

#include <string>
#include <iostream>

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 <string>
#include <iostream>
#include <sstream>
#include <locale>

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.

解决方案

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

#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;
  }
}

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

这篇关于如何将wstring转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆