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. JAVA笔记1-00

    package chapter1; public class Demo1 { public static void main(String[] args) { System.out.println(& ...

  2. 基于HCE移动支付研究报告

    1. 概念 HCE(host-based card emulation),即基于主机的卡模拟.在一部配备NFC功能的手机实现卡模拟,目前有两种方式:一种是基于硬件的,称为虚拟卡模式(Virtual C ...

  3. 封装fastjson为spring mvc的json view

    可以将其中的main方法删掉.测试用的.我测试的结果是,jackson比fastjson快. fastjson是1.1.36 jackson是2.2.3 jdk是1.7.40,client cpu是i ...

  4. VS2010启动奔溃

    最近由于项目需要,装了git,需要用bitbucket管理源代码.没想到装了git得插件后 vs2010不能启动,卸载重新安装也行,修复也不行. 解决方案: 删除 目录C:\Users\用户名\App ...

  5. linux之SQL语句简明教程---SUBSTRING

    SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分.这个函数的名称在不同的资料库中不完全一样: MySQL: SUBSTR( ), SUBSTRING( ) Oracle: S ...

  6. 电子科大POJ "敲错键盘"

    C-sources: #include<stdio.h> #define N 20 int main() { int i,j; ]={'Q','W','E','R','T','Y','U' ...

  7. nm命令中符号类型详解

    摘自http://blog.csdn.net/solmyr_biti/article/details/6565479 nm命令介绍的很多,但大多不介绍其函数符号标志的含义.最近在调试动态库时常用到,其 ...

  8. lhgDialog

    应用到你的项目 如果您使用独立版本的lhgDialog窗口组件,您只需在页面head中引入lhgcore.lhgdialog.min.js文件,4.1.1+版本做了修改可以和jQuerya库同时引用, ...

  9. 如何使用Storyboard创建UIPageViewController

    之前我们已经讲过UIPageViewController,那篇文章演示了如何使用Interface Builder创建UIPageViewController.为了适配iOS7和Xcode5,我们重新 ...

  10. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...