MFC中CString 与 std::string 相互转化

CString实际是CStringT, 也就是模板类,

在UNICODE环境下,实际是CStringW,

在多字符集环境下,实际是CStringA

std::string就是多字符集的.

UNICODE环境下

  • CStringW-->std::string

CString实际是CStringW,要转换成多字符集,需进行转码。使用WideCharToMultiByte 转换成多字符集,然后再构造std::string

  • std::string-->CStringW

因为CStringT模板类已经自动做了 char* 到 wchar_t* 的转码。

  • 实例
//使用Unicode 字符集
CString strCS("HelloWorld");
USES_CONVERSION;
std::string strS(W2A(strCS)); //CString-->std::string
CString strCStemp;
strCStemp = strS.c_str();//std::string-->CString

注意:std::string-->CString时,不可以写在同一行:

CString strCStemp = strS.c_str();//ERROR

多字符集

CString 实际就是CStringA.

//CStringA-->std::string
CString strCS("HelloWorld");
std::string strS;
strS = strCS.GetBuffer(); //std::string-->CStringA
CString strCStemp = strS.c_str();//注意,可写在同一行

CString 与 std::string 相互转化的更多相关文章

  1. CString与std::string unicode下相互转化

      1. CString to string CString str = L"test"; CString stra(str.GetBuffer(0)); str.ReleaseB ...

  2. CString转换成std::string

    unicode的编码格式: CString strCS; std::string strSTD =  CT2A(strCS.GetBuffer()); 其他的编码格式: CString strCS; ...

  3. CString std::string相互转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  4. 实战c++中的string系列--std::string与MFC中CString的转换

    搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...

  5. std::string 和 CString问题

    std::string stdTemp; CString strTemp; strTemp = stdTemp;    ;//这一步直接赋值可不可以 因为CString可以接受const char*的 ...

  6. c++之常见数据类型(int,double,float,long double long long 与std::string之间)相互转换(含MFC的CString、含C++11新特性函数)

    --- 已经通过初步测试---- ------------------ 下面的是传统常见数据类型的转换(非c++11)---------------  std::string 与其他常用类型相互转换, ...

  7. 17.Letter Combinations of a Phone Number (char* 和 string 相互转化)

    leetcode 第17题 分析 char*和string相互转化 char*(或者char)转string 可以看看string的构造函数 default (1) string(); copy (2 ...

  8. C++: std::string 与 Unicode 结合

    一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于_UNICODE 的值: // abridged from tc ...

  9. C++: std::string 与 Unicode 如何结合?

    关键字:std::string Unicode 转自:http://www.vckbase.com/document/viewdoc/?id=1293 一旦知道 TCHAR 和_T 是如何工作的,那么 ...

随机推荐

  1. BeyondCompare常用功能图解

    http://jingyan.baidu.com/article/066074d68f847ec3c31cb05a.html http://lovesoo.org/use-file-compariso ...

  2. HTML5 服务器发送事件(Server-Sent Events)介绍

    w3cschool菜鸟教程 Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获取来自服务器的更新. 以前也可能做到这一点,前提是网页不得不询问是否有可用的更新 ...

  3. Codeforces 306B

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  4. ubuntu gnome vnc

    1,安装登陆管理器 --apt-get install gdm (还可以为kdm/xdm) lightdm,display manager

  5. Gunner II(二分,map,数字转化)

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. Select标签 依据value值默认选中 Jquery

    网上找了非常多都是错的,不行的. 以下方法能够的 <script type="text/javascript"> $(document).ready(function( ...

  7. STL的移动算法

    要在自己定义类型中使用移动算法.须要在元素中提供移动赋值运算符.移动赋值运算符和std::move()详见<c++高级编程>第9章 class mystring { public: str ...

  8. jquery单页网站导航插件One Page Nav

    这是一个轻量级的jQuery的单页网站导航插件.增加了单击后平滑滚动导航和当你浏览不同的部分时自动选择正确的导航项. changeHash: false, 改变当用户单击导航,就改变changeHas ...

  9. Error 56: …… VPN Service has not been started

    vpn service在系统服务中名为Cisco System, Inc. VPN Service(cvpnd),异常情况下,该服务时停止的,并且手动启动该服务后没几秒,就被kill了.根据Zhiga ...

  10. Maximum & Minimum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...