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. Web学习

    http://book.2cto.com/201309/31936.html http://alvinalexander.com/ 查看锁表进程SQL语句1: select sess.sid,     ...

  2. Delphi窗体最大化按钮不可用情况下的最大化

    最大化按钮不可用,而且窗体最大化,我以前一直这样设置:在Object Inspector下把BorderIcons属性下的biMaximize属性设置为False,然后把WindowState属性设置 ...

  3. Delphi获取当前系统时间(使用API函数GetSystemTime)

    在开发应用程序时往往需要获取当前系统时间.尽管Y2K似乎已经平安过去,但在我们新开发的应用程序中还是要谨慎处理“时间”问题. 在<融会贯通--Delphi4.0实战技巧>(以下简称“该书” ...

  4. [IOS ] - INFO

    Chart in ios (native) https://github.com/kevinzhow/PNChart Web Page performance - YShow https://deve ...

  5. 在mangento后台调用wysiwyg编辑器

    在mangento后台调用操蛋的wysiwyg编辑器: 1.在头部加载TincyMCE protected function _prepareLayout() {     parent::_prepa ...

  6. 9款极具创意的HTML5/CSS3进度条动画(免积分下载)

    尊重原创,原文地址:http://www.cnblogs.com/html5tricks/p/3622918.html 免积分打包下载地址:http://download.csdn.net/detai ...

  7. 服务端API的OAuth认证实现

    http://stackoverflow.com/questions/12499602/body-joints-angle-using-kinect?rq=1 新浪微博跟update相关的api已经挂 ...

  8. Linux下patch打补丁命令

    此命令用于为特定软件包打补丁,他使用diff命令对源文件进行操作. 基本命令语法: patch [-R] {-p(n)} [--dry-run] < patch_file_name p:为pat ...

  9. UINavigationController技巧<一>——修改返回按钮的标题

    UINavigationController 一般push到另一界面后,返回按钮标题便是上一页面的title,但是对于push的第一页或者是上一页面没有title的,返回按钮标题便是默认back,如图 ...

  10. CM_RESOURCE_LIST structure

    The CM_RESOURCE_LIST structure specifies all of the system hardware resources assigned to a device. ...