Codejie's C++ Space

Using C++

How to convert string to wstring?

     来源:http://www.codeguru.com/forum/archive/index.php/t-193852.html

The copy() function does not automatically make room for the destination, so you must make sure that you have enough room in the wstring.

Why not just write a function to 
do the conversion?

#include 
<string>
#include 
<algorithm>

// Prototype for conversion functions
std::wstring StringToWString(const std::string& s);
std::
string WStringToString(const std::wstring& s);

std::wstring StringToWString(
const std::string& s)
{
std::wstring temp(s.length(),L
' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}


std::
string WStringToString(const std::wstring& s)
{
std::
string temp(s.length(), ' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}

using namespace std;

int main()
{
string s1 = "Hello";
wstring s2 
= StringToWString(s1);
s1 
= WStringToString(s2);
return 0;
}

Regards,

Paul McKenzie

posted on 2009-03-27 12:35 codejie 阅读(1612) 评论(6)  编辑 收藏 引用 所属分类: Resource

评论

# re: How to convert string to wstring? 2009-03-27 13:03 陈梓瀚(vczh)

你试试汉字。  回复  更多评论   

# re: How to convert string to wstring? 2009-03-27 17:56 codejie

么问题。  回复  更多评论   

# re: How to convert string to wstring? 2009-03-27 22:59 陈梓瀚(vczh)

你如何判断他没事  回复  更多评论   

# re: How to convert string to wstring?[未登录] 2009-03-29 23:03 codejie

可以正常读取一个中文文件名的MP3文件。你有什么疑问吗?  回复  更多评论   

# re: How to convert string to wstring?[未登录] 2013-11-26 01:19 烟圈

int main()
{
//string s1 = "";
//wstring s2 = StringToWString(s1);
string s1 = WStringToString(L"中国");

cout <<s1 <<endl;
return 0;
}

试试肯定不行。。

  回复  更多评论   

# re: How to convert string to wstring? 2013-11-26 09:24 codejie

@烟圈
会不会是'cout'用的不对呢?  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜