C++中CString string char* char 之間的字符轉(zhuǎn)換(多種方法)
首先解釋下三者的含義
CString 是一種很有用的數(shù)據(jù)類型。它們很大程度上簡(jiǎn)化了MFC中的許多操作(適用于MFC框架),使得MFC在做字符串操作的時(shí)候方便了很多。需要包含頭文件#include <afx.h>
C++是字符串,功能比較強(qiáng)大。要想使用標(biāo)準(zhǔn)C++中string類,必須要包含#include <string>// 注意是<string>,不是<string.h>,帶.h的是C語(yǔ)言中的頭文件。Char * 專門用于指以'\0'為結(jié)束的字符串.
以下方法來進(jìn)行轉(zhuǎn)換:
// CharConvert.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include<iostream>
#include<afx.h>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//此方法適用于“多字節(jié)” 否則會(huì)出現(xiàn)'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 轉(zhuǎn)換成string
// CString c_str1("Hello Zhengzhou University");
//string str;
//str=c_str1.GetBuffer(0);
//c_str1.ReleaseBuffer(); //否則就沒有釋放緩沖區(qū)所占的空間
//cout<<str<<endl;
//cout<<"\n"<<c_str1<<endl;
//string 轉(zhuǎn)換成CString
/*string str1("Hello College of Information Engineering");
CString c_str2;
c_str2=str1.c_str(); //c_str():生成一個(gè)const char*指針,指向以空字符終止的數(shù)組。
cout<<"the CString is "<<c_str2<<endl;
cout<<"the string is "<<str1<<endl;*/
//string轉(zhuǎn)換成const char*
//方法一:
//string str2("Hello College of Information Engineering");
//const char * str3; //常數(shù)指針
//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 直接轉(zhuǎn)換成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,往后數(shù)7個(gè),相當(dāng)于[ buffer[6], buffer[6+7] )
// buffer[length]='\0'; //加上'\0'使得buffer就到buffer[length]為止;
// cout <<"buffer contains: " << buffer <<endl;
//char * 轉(zhuǎn)換成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,不是有一個(gè)\0嗎
//char * 到CString
////char * c_str1="zzu";
////CString str; //可以直接轉(zhuǎn)換,因?yàn)镃String存在重載(在多字節(jié)字符集下適用)
////str.Format("%s",c_str1);
////cout<<"the c_str1 is"<<c_str1<<endl;
////cout<<"the CString is"<<str<<endl;
//方法1:使用API:WideCharToMultiByte進(jìn)行轉(zhuǎn)換(使用過,有效)
//CString str= CString("This is an example!");
//int n = str.GetLength(); //按字符計(jì)算,str的長(zhǎng)度
//int len = WideCharToMultiByte(CP_ACP,0,str,n,NULL,0,NULL,NULL);//按Byte計(jì)算str長(zhǎng)度
//char *pChStr = new char[len+1];//按字節(jié)為單位
//WideCharToMultiByte(CP_ACP,0,str,n,pChStr,len,NULL,NULL);//寬字節(jié)轉(zhuǎn)換為多字節(jié)編碼
// pChStr[len] = '\0';//不要忽略末尾結(jié)束標(biāo)志
//用完了記得delete []pChStr,防止內(nèi)存泄露
return 0;
}
同時(shí)需要注意的是,我們?cè)谄匠蓪懗绦驎r(shí),最好搞清我們的編譯環(huán)境中的編碼方式,不同的編碼方式可以會(huì)導(dǎo)致一些字符轉(zhuǎn)換失敗,當(dāng)我們編程開始,就要想好在哪個(gè)編碼方式下進(jìn)行,以免最后出現(xiàn)換編碼方式了,代碼卻出現(xiàn)很多錯(cuò)誤(很讓人頭疼),比如sqlite數(shù)據(jù)庫(kù)中的中文字符亂碼問題就是由于編碼方式和數(shù)據(jù)庫(kù)中默認(rèn)的編碼方式不一致。這點(diǎn)在我們寫程序時(shí)一定謹(jǐn)記。
MFC中char*,string和CString之間的轉(zhuǎn)換補(bǔ)充
一、 將CString類轉(zhuǎn)換成char*(LPSTR)類型
方法一,使用強(qiáng)制轉(zhuǎn)換。例如:
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();
// 使用完后及時(shí)釋放,以便能使用其它的CString成員函數(shù)
CString str = "ABCDEF";
char *pBuf = str,GetBuffer( 0 );
str.ReleaseBuffer();
二、 string轉(zhuǎn)char*
string 是c++標(biāo)準(zhǔn)庫(kù)里面其中一個(gè),封裝了對(duì)字符串的操作
把string轉(zhuǎn)換為char* 有3種方法:
1。data(),返回沒有”\0“的字符串?dāng)?shù)組
如:
string str="abc";
char *p=str.data();
2.c_str 返回有”\0“的字符串?dāng)?shù)組
如:string str="gdfd";
char *p=str.c_str();
3 copy
比如
string str="hello";
char p[40];
str.copy(p,5,0); //這里5,代表復(fù)制幾個(gè)字符,0代表復(fù)制的位置
*(p+5)='\0'; //要手動(dòng)加上結(jié)束符
cout < < p;
三、 字符串string轉(zhuǎn)換為其它數(shù)據(jù)類型
temp="123456";
1)短整型(int)
i = atoi(temp);
2)長(zhǎng)整型(long)
l = atol(temp);
3)浮點(diǎn)(double)
d = atof(temp);
string s; d= atof(s.c_str());
4)BSTR變量
BSTR bstrValue = ::SysAllocString(L"程序員");
...///完成對(duì)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*轉(zhuǎn)換為string
如果要把一個(gè)char 轉(zhuǎn)換成string, 可以使用 string s(char *);
五、string 轉(zhuǎn)CString
CString.format("%s", string.c_str());
六、char 轉(zhuǎn)CString
CString.format("%s", char*);
七、 CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否則就沒有釋放緩沖區(qū)所占的空間.
八、CString互轉(zhuǎn)int
將字符轉(zhuǎn)換為整數(shù),可以使用atoi、_atoi64或atol。
而將數(shù)字轉(zhuǎn)換為CString變量,可以使用CString的Format函數(shù)。如
CString s;
int i = 64;
s.Format("%d", i)
相關(guān)文章
MATLAB算法技巧和實(shí)現(xiàn)斐波那契數(shù)列的解決思路
這篇文章主要介紹了MATLAB算法技巧和實(shí)現(xiàn)斐波那契數(shù)列,這篇主要說一下自己在算法設(shè)計(jì)課上用matlab做的兩道算法題,題目解起來都比較簡(jiǎn)單,但是需要些技巧,需要的朋友可以參考下2022-12-12
C/C++實(shí)現(xiàn)Windows注冊(cè)表的基本操作
Windows注冊(cè)表(Registry)是Windows操作系統(tǒng)中用于存儲(chǔ)系統(tǒng)配置信息、用戶設(shè)置和應(yīng)用程序數(shù)據(jù)的一個(gè)集中式數(shù)據(jù)庫(kù),本文主要為大家介紹了C++對(duì)注冊(cè)表的基本操作,感興趣的小伙伴可以了解下2023-11-11
C++的靜態(tài)聯(lián)編和動(dòng)態(tài)聯(lián)編詳解
這篇文章主要介紹了C++的靜態(tài)聯(lián)編和動(dòng)態(tài)聯(lián)編詳解,對(duì)于深入理解C++編譯運(yùn)行原理有很大幫助,需要的朋友可以參考下2014-07-07
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)系列篇二叉樹的遍歷
本章將會(huì)詳細(xì)講解二叉樹遍歷的四種方式,分別為前序遍歷、中序遍歷、后續(xù)遍歷和層序遍歷。在學(xué)習(xí)遍歷之前,會(huì)先帶大家回顧一下二叉樹的基本概念2022-02-02
解決gcc編譯報(bào)錯(cuò)unknown type name ‘bool‘問題
這篇文章主要介紹了解決gcc編譯報(bào)錯(cuò)unknown type name ‘bool‘問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
C語(yǔ)言小游戲之小熊跳板功能的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言小游戲之小熊跳板功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))基本操作的實(shí)現(xiàn)詳解
鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文主要和大家聊聊C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))的基本操作,感興趣的小伙伴可以了解一下2022-11-11

