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. 多个项目使用NET Core

    .NET Core系列 :3 .使用多个项目   通过前面的两篇文章,我们已经知道如何创建新的项目,如何生成并运行我们的应用程序,也知道(大致) project.json 文件中的内容是什么意思.但大 ...

  2. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

  3. WAMP环境搭建步骤

    在d盘创建myServer文件夹 然后apache2.2 mysql php-5.3.5  1 安装apache2.2 2 安装php-5.3.5 3 apache与php环境的整合 1)在httpd ...

  4. wordpress All in one Seo

    原文地址:http://www.7adesign.com/155.html WordPress插件All-in-one-seo-pack详细设置: I enjoy this plugin and ha ...

  5. hdu 1536 S-Nim_求sg值模版

    题意:给你很n堆石头,k代表你有k种拿法,然后给出没堆石头的数量,求胜负 直接套用模版 找了好久之前写的代码贴上来 #include<iostream> #include<algor ...

  6. xml中,button改变背景颜色方法

    在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下. 方法一尝试了好多遍才好,要点在于,在sele ...

  7. Android工程师面试准备知识点

    听+7哥说,把下面的全弄懂,面试必过,所以我就试着去把所有题目补充了一下,各位能耐的网友们,如果有吐槽或者补充的尽管给我留言,在这里谢过大家了. 1.   android的多线程如何通信 答:Andr ...

  8. 如何实现在O(n)时间内排序,并且空间复杂度为O(1)

    对于常见的排序算法,很难做到在O(n)时间内排序,并且空间复杂度为O(1),这里提供了一种方法可以达到要求. 可以使用哈希排序的思想,也就是将所有的数哈希到哈希表中,实现排序.具体的算法思想是,求出这 ...

  9. R-plot

    颜色.图例和线 在散点图中添加信息.图例以及回归线. 模拟数据 #模拟数据 dat <- data.frame(X = runif(100,-2,2),T1 = gl(n=4,k=25,labe ...

  10. 利用gridview实现计时消费,有点复杂,谁有好的方法可以讨论一下...

    这是前段时间做项目遇到的一个问题,做出来的效果图如下, 由会员id查询出会员来,然后开始计费.然后点击结束消费,传到别的页面,主要就是结束时间和开始时间的一个时间差. 用到的数据表设计视图如下, 为了 ...