How to convert string to wstring?
2014-04-08 10:47
169 查看
How to convert string to wstring? - Codejie's C++ Space - 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 阅读(655) 评论(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;
}
试试肯定不行。。
来源: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 阅读(655) 评论(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;
}
试试肯定不行。。
相关文章推荐
- how to convert wstring to string
- how to convert wstring to string
- how to convert wstring to string
- how to convert wstring to string
- 【Z一贴】C++ String: How to convert a string into a numeric type?
- How to convert QString to std::string?
- 如何将String类型的数组转换成ArrayList(How to convert a string array to arraylist)(转)
- How to convert enum to string with boost library
- 如何将String类型的数组转换成ArrayList(How to convert a string array to arraylist)
- how convert large HEX string to binary array ?
- Convert std::string to std::wstring or LPCWSTR
- How to convert a byte to its binary string representation
- How to convert char array to String in Java
- How to properly convert SecureString to String
- How can I convert printStackTrace message to a string?
- How Do I Convert an Integer to a String in Excel VBA
- How to convert a String to DateTime
- How to convert std::string to LPCWSTR in C++ (Unicode)
- How to convert any valid date string to a DateTime.
- How to convert string to Guid type.