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. 触发按钮改变panel面板上的小圆圈颜色

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TouChaCol implements ...

  2. setObject与setValue的区别

    在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有.1.先看看setVa ...

  3. 3种SQL语句分页写法

    在开发中经常会使用到数据分页查询,一般的分页可以直接用SQL语句分页,当然也可以把分页写在存储过程里,下面是三种比较常用的SQL语句分页方法,下面以每页5条数据,查询第3页为例子: 第一种:使用not ...

  4. IsNullOrEmpty和s == null || s.Length == 0哪个快

    在写扩展方法时,看到有人用==null这个方法,说快,上网找了些资料,最后在csdn的博客上看到了一篇文章,说实测是后两者快,于是我也试着做了一个程序运行了一下,却发现这样的结果: 我测试了一个,发现 ...

  5. scrapy爬虫初体验

    scrapy是一个python的爬虫框架,用于提取结构性数据.在这次宝贝计划1的项目中要用到一些数据.但四处联系后各方可能因为一些隐私问题不愿提供数据信息.这样我们只能自己爬取,存入数据库,再进行调用 ...

  6. 在什么情况下使用exist和in

    http://www.itpub.net/thread-406784-4-1.htmlYou Asked (Jump to Tom's latest followup) Tom: can you gi ...

  7. C# 懒人常用异步方法

    Winform this.Invoke(new Action(() => { })); Wpf this.Dispatcher.Invoke(DispatcherPriority.Normal, ...

  8. 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 ...

  9. JavaWeb核心编程之(三.6)HttpServlet

    之前都是集成的Servlet真的太过于繁琐了, Servlet接口提供了一个实现类 为HttpServlet  只要实现doGet 和doPost方法就可以了 仍然以一个表单为例 新建一个web工程 ...

  10. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...