在Unicode环境下用以下转换:

CString z_strCurtTime = _T("");

// 获取当前时间
CTime z_tCurTime = CTime::GetCurrentTime();
z_strCurtTime = z_tCurTime.Format("%Y-%m-%d %H:%M:%S");
int z_len =WideCharToMultiByte(CP_ACP,0,z_strCurtTime,-1,NULL,0,NULL,NULL);
char *z_cCurTime =new char[z_len +1];
WideCharToMultiByte(CP_ACP,0,z_strCurtTime,-1,z_cCurTime,z_len,NULL,NULL );

1 前言

今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结.

首先大家得清楚一件事,一般在网上提出问题的人大部分使用的都是VC,那么你就应该知道,在VC下编程,工程属性中有一属性Charecter Set属性,其值可以设置为Use Multi-Byte Charecter Set 和 Use Unicode Charecter Set 这两种选择,具默认情况下工程是采用了Use Unicode Charecter Set选项.如我使用的VS2010的工程属性中如下:

VC在处理CString类型字符时,在这两种不种选择的处理结果也是完全不一样的,而网上那么答复大都是针对假设提问者是使用了Use Mult-Byte Chracter Set的前提下,但大多提这个问题的人都是使用了后者的情况的人.

暂且将Use Mult-Byte Chracter Set称之为宽字节字符模式,而Use Unicode Charecter Set称之为Unicode编码模式.

2 宽字节字符模式

首先讨论一下宽字符字符模式下的CStirng与Char *之间的互转,在这种情况下互换很简单:

2.1 CString -->char *

如下:

  1. CString str1 ="123";
  2. char *p =(LPSTR)(LPCSTR)str1;

但好像官方并不建议这么做,而建议采用下面这种方式:

  1. CString str1 ="123";
  2. char *t1 =str1.GetBuffer(str1.GetLength());
  3. str1.ReleaseBuffer();
  4. //do something with t1

网上也有人说是这样t1 =str1.GetBuffer(0);但其实我在实测时并没发现str1.GetBuffer(str1.GetLenth())与str.GetBuffer(0)返回值有啥区别,MSDN中相应说明如下:

  1. CString::GetBuffer
  2. LPTSTR GetBuffer( int nMinBufLength );
  3. throw( CMemoryException );
  4. Return Value
  5. An LPTSTR pointer to the object’s (null-terminated) character buffer.
  6. Parameters
  7. nMinBufLength
  8. The minimum size of the character buffer in characters. This value does not include space for a null terminator.
  9. Remarks
  10. Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
  11. If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
  12. The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
  13. The buffer memory will be freed automatically when the CString object is destroyed.
  14. Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.

由上可知,GetBuffer的参数nMinBufLength为最小缓冲区长度,但实际结果没啥区别...

2.2 char * -->CString

  1. char *str ="aaaa"
  2. CString str1(str);
  3. //...

2.3 CString -->int

在宽字符字符模式下,这个非常简单:

  1. CString str1 ="123";
  2. int i =atoi(str1);
  3. //do something with i

2.4 int -->CString

  1. int i =100;
  2. CString str;
  3. str.Format("%d",i);
  4. //...

3 Unicode编码模式

3.1 CString -->char *

在这种情况下,上述所说的转化全是浮云,目前只发现可以用WideCharToMultiByte函数来实现.

如下 :

  1. CString str1 =_T("123");
  2. int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL);
  3. char *ptxtTemp =new char[len +1];
  4. WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL );
  5. //...
  6. delete[] ptxtTemp;

3.2 char * -->CString

还是可以如下:

  1. char *p ="test";
  2. CString str(p);
  3. //...

3.3 CString -->int

在这种情况下atoi不再适用,其实可以用swscanf,如下:

  1. CString str2 =_T("100");
  2. int i;
  3. swscanf(str2,_T("%d"),&i);

3.4 int -->CString

这个其实最简单了,如下:

  1. int j =100;
  2. CString str3;
  3. str3.Format(_T("%d"),j);

CString转std::wstring

std::wstring str = filename.GetString();

std::wstring转CString

CString str( filename.c_str() );

4 结束

另外,有关ANSI与Unicode之间的转换UTF-8与Unicode之间的转换可以参与下面这个链接:

http://www.cnblogs.com/gakusei/articles/1585211.html

MFC string char cstring 类型转换的更多相关文章

  1. C语言中string char int类型转换

    C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...

  2. C++ 中int,char,string,CString类型转换

      1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...

  3. C++ 中 string, char*, int 类型的相互转换

    一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...

  4. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  5. mfc CString,string,char* 之间的转换

    知识点: CString转char*,string string转char*,CString char* 转CString,string 一.CString转char*,string //字串转换测试 ...

  6. MFC/C++/C中字符类型CString, int, string, char*之间的转换

    1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...

  7. CString string char* char 之间的字符转换(多种方法)

    在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换.首先解释下三者的含义. CString 是一种很有用的数据类型.它们很大程度上简化了MF ...

  8. 转:char*, char[] ,CString, string的转换

    转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...

  9. 【转】CString,string,char*综合比较

    (一)  概述 1.string和CString均是字符串模a板类: 2.string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: 3.CString(typedef CString ...

随机推荐

  1. nginx(三)-动静分离

    什么叫动静分离 所谓动静分离就是说我们的图片,css,js之类的文件都交给nginx来处理,nginx处理不了的,比如jsp就交给tomcat来处理. 有人计算过,nginx代理处理静态请求远远优于t ...

  2. my35_MGR添加新节点

    MGR添加节点主要涉及以下两个参数 group_replication_group_seeds    #可以动态修改 group_replication_ip_whitelist      #需要关闭 ...

  3. 《Paxos Made Simple》翻译(转)

    1 Introduction 可能是因为之前的描述对大多数读者来说太过Greek了,Paxos作为一种实现容错的分布式系统的算法被认为是难以理解的.但事实上,它可能是最简单,最显而易见的分布式算法了. ...

  4. SpringBoot 整合 ActiveMq

    消息队列,用来处理开发中的高并发问题,通过线程池.多线程高效的处理并发任务. 首先,需要下载一个ActiveMQ的管理端:我本地的版本是 activemq5.15.8,打开activemq5.15.8 ...

  5. A problem has been detected and windows has been shut down to prevent damage

    问题描述 问题解决 参考百度经验, 未解决,重装系统,U盘启动解决.过程可以参考上一篇博文.

  6. cadence help文件库调出指令 :cdnshelp

    cadence help文件库调出指令 :cdnshelp 指令参数记录: Verilog 添加可编译文件后缀名 -vlog_ext +.h,.vh Verilog1995 添加可编译文件后缀名 -v ...

  7. 局域网电脑之间ping不通解决办法

    局域网电脑之间ping不通一般都是防火墙的原因.解决办法有以下两种方法 1 第一种方法 1 找到Windows防火墙 2点击更改通知设置 3 关闭专用网络防火墙 现在就可以ping通这台机器了. 2  ...

  8. 配置nginx服务器全局命令

    service nginx start|stop|reload 报错:Failed to reload nginx.service: Unit not found.[解决方案] 在执行service ...

  9. Swift编程权威指南第2版 读后收获

    自从参加工作一直在用OC做iOS开发.在2015年的时候苹果刚推出swift1.0不久,当时毕竟是新推出的语言,大家也都很有激情的学习.不过在学完后发现很难在实际项目中使用,再加上当时公司项目都是基于 ...

  10. Scrapy框架学习(一)Scrapy框架介绍

    Scrapy框架的架构图如上. Scrapy中的数据流由引擎控制,数据流的过程如下: 1.Engine打开一个网站,找到处理该网站的Spider,并向该Spider请求第一个要爬取得URL. 2.En ...