首页 > 编程 > C++ > 正文

C++中CString string char* char 之间的字符转换(多种方法)

2020-01-26 13:55:20
字体:
来源:转载
供稿:网友

首先解释下三者的含义

CString 是一种很有用的数据类型。它们很大程度上简化了MFC中的许多操作(适用于MFC框架),使得MFC在做字符串操作的时候方便了很多。需要包含头文件#include <afx.h>

C++是字符串,功能比较强大。要想使用标准C++中string类,必须要包含#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件。Char * 专门用于指以'/0'为结束的字符串.

以下方法来进行转换:

// CharConvert.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<afx.h>#include<string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){	//此方法适用于“多字节” 否则会出现'strcpy' : cannot convert parameter 2 from 'unsigned short *' to 'const char *'	/*CString str("Hello zzu");	cout<<"the CString is "<<str<<endl;	char a[100];	strcpy(a,str);	cout<<"the convert to char * is "<<a<<"(char *)"<<endl;*/	//CString 转换成string	//  CString c_str1("Hello Zhengzhou University");	//string str;	//str=c_str1.GetBuffer(0);	//c_str1.ReleaseBuffer();  //否则就没有释放缓冲区所占的空间	//cout<<str<<endl;	//cout<<"/n"<<c_str1<<endl;	//string 转换成CString	/*string str1("Hello College of Information Engineering");	CString c_str2;	c_str2=str1.c_str(); //c_str():生成一个const char*指针,指向以空字符终止的数组。	cout<<"the CString is "<<c_str2<<endl;	cout<<"the string is "<<str1<<endl;*/	//string转换成const char*	//方法一:	//string str2("Hello College of Information Engineering");	//const char * str3;  //常数指针	//str3=str2.c_str();	//cout<<"string  is  "<<str2<<endl;	//cout<<"const char is "<<str3<<endl;		//方法二: // /* string str("Hello College of Information Engineering");	//const char * c_str;	//c_str=str.data();	//cout<<"string is  "<<str<<endl;	//cout<<"const char is"<<c_str<<endl;*/				//string 直接转换成char*	///*string s1 = "abcdefg";	//char *data;	//int len = s1.length();	//data = (char *)malloc((len)*sizeof(char));	//s1.copy(data,len,0);	//cout<<len<<endl;	//cout<<data<<endl;*/	      	//string.copy()的用法	  //size_t length;  //  char buffer[8];  // string str("Test string......");  //    // length=str.copy(buffer,7,6);    //从buffer6,往后数7个,相当于[ buffer[6], buffer[6+7] )  // buffer[length]='/0';            //加上'/0'使得buffer就到buffer[length]为止;   // cout <<"buffer contains: " << buffer <<endl;  //char * 转换成string	//char *到string  /* char * c_str="zzu";	string str(c_str);	cout<<"the c_str "<<c_str<<endl;	cout<<"the string is"<<str<<"and length is "<<str.length()<<endl;*/	//char a[]="asd";	//cout<<strlen(a)<<endl; //为什么显示的是3,不是有一个/0吗	//char * 到CString  	////char * c_str1="zzu";	////CString str; //可以直接转换,因为CString存在重载(在多字节字符集下适用)	////str.Format("%s",c_str1);	////cout<<"the c_str1 is"<<c_str1<<endl;		////cout<<"the CString is"<<str<<endl;	//方法1:使用API:WideCharToMultiByte进行转换(使用过,有效)   //CString str= CString("This is an example!");   //int n = str.GetLength(); //按字符计算,str的长度   //int len = WideCharToMultiByte(CP_ACP,0,str,n,NULL,0,NULL,NULL);//按Byte计算str长度   //char *pChStr = new char[len+1];//按字节为单位   //WideCharToMultiByte(CP_ACP,0,str,n,pChStr,len,NULL,NULL);//宽字节转换为多字节编码   // pChStr[len] = '/0';//不要忽略末尾结束标志   //用完了记得delete []pChStr,防止内存泄露	return 0;}

同时需要注意的是,我们在平成写程序时,最好搞清我们的编译环境中的编码方式,不同的编码方式可以会导致一些字符转换失败,当我们编程开始,就要想好在哪个编码方式下进行,以免最后出现换编码方式了,代码却出现很多错误(很让人头疼),比如sqlite数据库中的中文字符乱码问题就是由于编码方式和数据库中默认的编码方式不一致。这点在我们写程序时一定谨记。

MFC中char*,string和CString之间的转换补充

一、    将CString类转换成char*(LPSTR)类型

方法一,使用强制转换。例如:

CString theString( "This  is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

方法二,使用strcpy。例如:

CString theString( "This  is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);

方法三,使用CString::GetBuffer。例如:

CString s(_T("This is a  test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p =  _T('/0');
s.ReleaseBuffer();
// 使用完后及时释放,以便能使用其它的CString成员函数

CString str = "ABCDEF";
char *pBuf = str,GetBuffer( 0 );
str.ReleaseBuffer();

二、     string转char*

string 是c++标准库里面其中一个,封装了对字符串的操作
把string转换为char* 有3种方法:
1。data(),返回没有”/0“的字符串数组
如:
string str="abc";
char  *p=str.data();
2.c_str 返回有”/0“的字符串数组
如:string  str="gdfd";
    char *p=str.c_str();
3 copy
比如
string  str="hello";
char p[40];
str.copy(p,5,0); //这里5,代表复制几个字符,0代表复制的位置
*(p+5)='/0';  //要手动加上结束符
cout < < p;

三、     字符串string转换为其它数据类型

temp="123456";
1)短整型(int)
i =  atoi(temp);
2)长整型(long)
l =  atol(temp);
3)浮点(double)
d =  atof(temp);
string s; d= atof(s.c_str());
4)BSTR变量
BSTR bstrValue =  ::SysAllocString(L"程序员");
...///完成对bstrValue的使用
SysFreeString(bstrValue);
5)CComBSTR变量
CComBSTR类型变量可以直接赋值
CComBSTR  bstrVar1("test");
CComBSTR bstrVar2(temp);
6)_bstr_t变量
_bstr_t类型的变量可以直接赋值
_bstr_t  bstrVar1("test");
_bstr_t bstrVar2(temp);

四、     Char*转换为string

如果要把一个char 转换成string, 可以使用 string s(char  *);

五、string 转CString 

CString.format("%s",  string.c_str());

六、char 转CString 

CString.format("%s", char*);

七、     CString -> string

string  s(CString.GetBuffer()); 
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

八、CString互转int

将字符转换为整数,可以使用atoi、_atoi64或atol。 
而将数字转换为CString变量,可以使用CString的Format函数。如 

CString s; int i = 64; s.Format("%d", i)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表