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. 转:C4项目中验证用户登录一个特性就搞定

    转:C4项目中验证用户登录一个特性就搞定   在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性.     // 摘要:    //     表示一个特性,该特性用于限制调用 ...

  2. 将数组适配到ListView

    public class TutListActivity extends ListActivity { @Override public void onCreate(Bundle savedInsta ...

  3. bug fix: openstack can not run swift for pyeclib and liberasurecode do not match

    最近在使用devstack 安装openstack nimble项目. nimble项目是一个专业的baremetal管理项目. 安装过程中,遇到这个问题. /opt/stack/swift/bin/ ...

  4. 优秀的目录文档内容查找,替换工具,可以飞快的帮助你查询大IIS日志哟。

    这,是一款飞速的目录文档中内容查找的工具. 它,飞快精准的帮助你查询到你想搜索的文档中的内容. 它,是一款由非常牛B,我都不晓得姓名的作者开发的,冒失是C++的windows应用. 你,非常需要他. ...

  5. iOS 创建上线证书

    1.制作上线证书需要准备一个付费的账号(99$),登陆https://developer.apple.com在最上方的位置点击Member Center进入登陆界面,在登陆界面输入付费的账号和密码进入 ...

  6. Bootstrap 简洁、直观、强悍、移动设备优先的前端开发框架,让web开发更迅速、简单。

    http://v3.bootcss.com/ 从2.x升级到3.0版本 Bootstrap 3并不向后兼容Bootstrap v2.x.下面章节列出的内容可以作为从v2.x升级到v3.0的通用指南.如 ...

  7. swift http请求返回json数据和分析

    1 AppDelegate.swift // // AppDelegate.swift // QQDemo // // Created by 赵超 on 14-6-21. // Copyright ( ...

  8. 系统分层 manager层意义

    manager用于控制事务,通常是这么说的,但是如果把事务空指针service可以的,但是有些时候,service加了Transaction注解之后,在加别的注解,可能导致Transaction失效. ...

  9. lamda表达式学习

    lamda表达式 “Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型. 格式:( 形参列表 ) => { 函数体 } 所有 Lambda 表达式 ...

  10. XPath在asp.net中查询XML

    .NET Framework 2.0中可以使用System.Xml.XPath命名空间下的类对XML文档进行基于路径的查询,在查询过程中需要构造类似SQL的查询字符串,该字符串遵循XPath语法.它由 ...