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);

4 结束

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

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

CString与char *互转总结的更多相关文章

  1. CString和char互转,十六进制的BYTE转CString

    CString转char: CString m_Readcard; char ReaderName[22]; strcpy((char*)&ReaderName,(LPCTSTR)m_Read ...

  2. 【转载】CString,string,char*之间的转换

    本文转自 <> 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌 ...

  3. 2、CString与string借助char *互转

    CString是MFC中的类,MFC前端界面中获得的字符串是CString类.标准C/C++库函数是不能直接对CString类型进行操作的. string是C++中的类. 安全性 CString &g ...

  4. CString转char * ,string

    CString头文件#include <afx.h> string头文件#include <string.h> 1.CString转char * CString cstr; c ...

  5. Cstring转char、string、int等数据类型的方法(转载)

    Cstring转char.string.int等数据类型的方法 (-- ::) 转载 标签: 杂谈 分类: VC CString 转char * CString cstr; char *p = (LP ...

  6. Unicode字符集下CString与char *转换 (解决中文乱码等)(转)

    UniCode 下 CString 转 char* 的方法的文章有很多,但是大部分都是在互相转载,看了那么多资料,仍然没有解决乱码的问题,后来从一个论坛的一条回复里面找到了正确的方法,特此拿出来与大家 ...

  7. c++ string 与 char 互转 以及base64

    c++ string 与 char 互转 很简单如下 ] = {'A','B','C','D','E'}; printf("%s\n",bts); //char to string ...

  8. CString,string,char*之间的转换(转)

    这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...

  9. CString 转 char*; wstring 转 string

    1. CString  转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...

随机推荐

  1. canvas制作简单动画

    在画布元素<canvas>中,除了绘制图形.图像.文字外,还可以制作一些简单的动画,制作过程十分简单,主要分为两步操作: 1.自定义一个函数,用于图形的移动或其他动作. 2.使用setIn ...

  2. 关于ubuntu 系统

    1. ubuntu怎么卸载自带的软件中心?sudo apt-get remove software-center 2. 重新安装ubuntu软件中心!sudo apt-get install --re ...

  3. Python 一路走来 HTML CSS Javascript

    前端三把利器 HTML          -标签 (成对写不容易忘记闭合)                     自闭和标签           标签里写个 xx=xx, 表示标签的属性       ...

  4. Repeater中添加按钮,点击按钮获取某一行的数据

    1.添加编辑按钮和删除按钮 <asp:Repeater ID="Repeater1" runat="server" onitemcommand=" ...

  5. C C++ OC iOS面试重点问题(一)

    C C++ OC iOS面试重点问题(一) 1.字符串常量需要加\0 2.逻辑运算 位操作(经典:实现两个数的交换) 3.关键字 4.引用和指针的区别和联系 5.如何引用一个已经定义过的全局变量?  ...

  6. contentSize、contentInset和contentOffset区别

    contentSize.contentInset和contentOffset区别 分类: iphone开发2011-12-05 21:49 23495人阅读 评论(4) 收藏 举报 uiviewios ...

  7. POJ 1755 Triathlon

    http://poj.org/problem?id=1755 题意:铁人三项,每个人有自己在每一段的速度,求有没有一种3条路线长度都不为0的设计使得某个人能严格获胜? 我们枚举每个人获胜,得到不等式组 ...

  8. 实现ECMAScript的引擎

    list of ECMAScript engines From Wikipedia, the free encyclopedia     An ECMAScript engine is a progr ...

  9. 3D打印论坛

    3D打印论坛:http://www.3done.cn http://www.03dp.com www.qjxxw.net/ http://www.3ddayin.net http://oa.zol.c ...

  10. Jetty直接调试,不用部署,不用弄一些杂七杂八的设置

    以前调试web程序的,搭建Tomcat实在是费劲,就想找一个比较简单的方式,我就想调试一下我写的某一个servlet形式,看到<how Tomcat works>这本书,才明白确实可以,不 ...