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

    试试肯定不行。。

How to convert string to wstring?的更多相关文章

  1. 基于标准库实现string和wstring的转换

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...

  2. string、wstring、cstring、 char、 tchar、int、dword转换方法(转)

    string.wstring.cstring. char. tchar.int.dword转换方法(转)   最近编程一直头痛这集中类型的转化,明知都可以转却总是记不住,不断的上网查来查去,在这里小结 ...

  3. svn: Can't convert string from 'UTF-8' to native encoding 的解决办法(转)

    http://www.cnblogs.com/xuxm2007/archive/2010/10/26/1861223.html svn 版本库中有文件是以中文字符命名的,在 Linux 下 check ...

  4. STL的string和wstring

    STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...

  5. 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误

    转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...

  6. [C++]C++标准里 string和wstring

    typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; 前者string是常用类型, ...

  7. svn: Can't convert string from 'UTF-8' to native encoding 的解决办法

    http://www.leakon.com/archives/610 http://www.toplee.com/blog/566.html http://svnbook.red-bean.com/e ...

  8. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  9. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下:    #ifndef USE_H_     ...

随机推荐

  1. ALM11客户端安装办法

    使用HP公司的QC11(ALM11.0)做项目缺陷管理,发现QC只支持IE7,IE8.其实可以通过安装ALM自已提供的浏览器就行了. 第一步:关闭UAC(用户账户控制) 第二步:打开ie,将ie-in ...

  2. gl.TexSubImage2D 使用遇到图片翻转的问题

    这2天在用gl.TexSubImage2D把几张小图转拼接成大图,如果在渲染物体之前拼接好就没有问题,但在开始渲染物体后拼接就会有问题.后来我做了2件事情来找原因, 1. 用拼好的图来画一个正方形,大 ...

  3. delphi 把多个线程的请求阻塞到另一个线程 TElegantThread

    本例是把多个线程访问数据库的请求,全部阻塞到一个线程. 这是实际编程中常见的一种问题. 示例源码下载,所需支持单元均在源码中,且附详细说明. TElegantThread 的父类是 TSimpleTh ...

  4. Dependency Walker使用说明

    Dependency Walker使用说明 标签: dllexewindowsvbqq工具 2010-03-29 11:10 25175人阅读 评论(22) 收藏 举报  分类: 基本常识(45)  ...

  5. codec ruby和json格式输出

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat geoip.conf input {stdin {} } filter { geoip { ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构

    switch什么时候用break,什么时候不用break 调用break:一次执行一个分支,输入一个数据,对应一个级别 不调用break:连续执行多个分支 if...else 可以处理任何情况,大于小 ...

  7. Longge的问题(欧拉,思维)

     Longge的问题 Submit Status Practice HYSBZ 2705 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一 ...

  8. jqery ajax读取json文件

    json文件数据 [ {"name":"哈哈··","email":"邮箱01","gender": ...

  9. css3 transiton

    div { width:100px; height:100px; background:yellow; transition-property:width; transition-duration:1 ...

  10. C# Socket服务端和客户端互相send和receive

    服务端           {          ];          ;                                       {                       ...