CString和基本数据类型的转换
2017-10-31 13:36
330 查看
1 前言
今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结.首先大家得清楚一件事,一般在网上提出问题的人大部分使用的都是VC,那么你就应该知道,在VC下编程,工程属性中有一属性Charecter Set属性,其值可以设置为Use Multi-Byte Charecter Set 和 Use Unicode Charecter Set 这两种选择,具默认情况下工程是采用了Use Unicode Charecter Set选项.如我使用的VS2010的工程属性中如下:
![](http://img.my.csdn.net/uploads/201301/25/1359122582_7710.jpg)
VC在处理CString类型字符时,在这两种不种选择的处理结果也是完全不一样的,而网上那么答复大都是针对假设提问者是使用了Use Mult-Byte Chracter Set的前提下,但大多提这个问题的人都是使用了后者的情况的人.
暂且将Use Mult-Byte Chracter Set称之为宽字节字符模式,而Use Unicode Charecter Set称之为Unicode编码模式.
2 宽字节字符模式(多字节字符集)
首先讨论一下宽字符字符模式下的CStirng与Char *之间的互转,在这种情况下互换很简单:2.1 CString –>char *
如下:[cpp] view plain copy print?CString str1 =“123”;
char *p =(LPSTR)(LPCSTR)str1;
CString str1 ="123"; char *p =(LPSTR)(LPCSTR)str1;
但好像官方并不建议这么做,而建议采用下面这种方式:
[cpp] view plain copy print?CString str1 =“123”;
char *t1 =str1.GetBuffer(str1.GetLength());
str1.ReleaseBuffer();
//do something with t1
CString str1 ="123"; char *t1 =str1.GetBuffer(str1.GetLength()); str1.ReleaseBuffer(); //do something with t1
网上也有人说是这样t1 =str1.GetBuffer(0);但其实我在实测时并没发现str1.GetBuffer(str1.GetLenth())与str.GetBuffer(0)返回值有啥区别,MSDN中相应说明如下:
[plain] view plain copy print?CString::GetBuffer LPTSTR GetBuffer( int nMinBufLength ); throw( CMemoryException ); Return Value An LPTSTR pointer to the object’s (null-terminated) character buffer. Parameters nMinBufLength The minimum size of the character buffer in characters. This value does not include space for a null terminator. Remarks 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. If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. 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. The buffer memory will be freed automatically when the CString object is destroyed. 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.
CString::GetBuffer LPTSTR GetBuffer( int nMinBufLength ); throw( CMemoryException ); Return Value An LPTSTR pointer to the object’s (null-terminated) character buffer. Parameters nMinBufLength The minimum size of the character buffer in characters. This value does not include space for a null terminator. Remarks 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. If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. 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. The buffer memory will be freed automatically when the CString object is destroyed. 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
[cpp] view plain copy print?char *str =“aaaa”CString str1(str);
//…
char *str ="aaaa" CString str1(str); //...
2.3 CString –>int
在宽字符字符模式下,这个非常简单:[cpp] view plain copy print?CString str1 =“123”;
int i =atoi(str1);
//do something with i
CString str1 ="123"; int i =atoi(str1); //do something with i
2.4 int –>CString
[cpp] view plain copy print?int i =100;CString str;
str.Format(”%d”,i);
//…
int i =100; CString str; str.Format("%d",i); //...
3 Unicode编码模式
3.1 CString –>char *
在这种情况下,上述所说的转化全是浮云,目前只发现可以用WideCharToMultiByte函数来实现.如下 :
[cpp] view plain copy print?CString str1 =_T(“123”);
int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL);
char *ptxtTemp =new char[len +1];
WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL );
//…
delete[] ptxtTemp;
CString str1 =_T("123"); int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL); char *ptxtTemp =new char[len +1]; WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL ); //... delete[] ptxtTemp;
3.2 char * –>CString
还是可以如下:[cpp] view plain copy print?char *p =“test”;
CString str(p);
//…
char *p ="test"; CString str(p); //...
3.3 CString –>int
在这种情况下atoi不再适用,其实可以用swscanf,如下:[cpp] view plain copy print?CString str2 =_T(“100”);
int i;
swscanf(str2,_T(”%d”),&i);
CString str2 =_T("100"); int i; swscanf(str2,_T("%d"),&i);
3.4 int –>CString
这个其实最简单了,如下:[cpp] view plain copy print?int j =100;
CString str3;
str3.Format(_T(”%d”),j);
int j =100; CString str3; str3.Format(_T("%d"),j);
相关文章推荐
- 关键字,标识符,数据类以及基本数据类型的转换
- java语言基础(59)——jdk5自动装箱和拆箱(基本数据类型与包装类之间的转换)
- Java 基本数据类型、类型转换
- Java 基本数据类型的包装类及其数据类型转换
- 数据类型转换(二)char*和CString的转换
- Python基本语法_强制数据类型转换
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
- Java的基本数据类型:8种基本类型、对应的包装类、引用数据类型及与String 之间的转换。
- 【java基础】在Java中实现基本数据类型与字符、字符串之间的转换
- JAVA基本数据类型转换 .
- Unit5 基本类型数据类型转换与字符串的使用
- Java中的移位操作以及基本数据类型转换成字节数组【收集】
- java基本数据类型及类型转换
- java基本数据类型之间的转换
- Java基本数据类型的转换
- Java核心技术知识点笔记—基本数据类型及转换
- JavaSE_2th_标识符、关键字、基本数据类型、类型转换、变量和常量
- 【Java】基本数据类型与封装类、字符串的相互转换
- (3)JavaScript基础(基本语法:变量与数据类型、类型转换、运算符、流程控制、函数、对象、自定义对象、原型)
- OC字符串和 C 语言基本数据类型的相互转换