卷1 入门CString与字符编码

来源:互联网 发布:军团要塞2 知乎 编辑:程序博客网 时间:2024/05/23 21:42

在MFC中字符串常用的是CString,当然在实际操作中,标准的是使用char*类型,所以我们免不了字符串之间的转换。下面就介绍常用的字符串的转换。

1)(Unicode编码下的转换)

CString转整型

UINT data=0;

_stscanf_s((LPCTSTR)value,_T("%d"),&data);


CString----->>string: 
CString cs("test"); 
string s; 
s = cs.GetBuffer(0); 

string --------->> CString: 
string s = "test"; 
CString cs; 
cs = s.c_str();


char*与CString是用的比较多的,我把它封装成了下面两个函数,应该可以看懂什么意思把~

CString pcharToCString(char * pChar){ int charLen = strlen(pChar); //计算pChar所指向的字符串大小,以字节为单位,一个汉字占两个字节  int len = MultiByteToWideChar(CP_ACP, 0, pChar, charLen, NULL, 0); //计算多字节字符的大小,按字符计算  wchar_t *pWChar = new wchar_t[len + 1]; //为宽字节字符数申请空间,  MultiByteToWideChar(CP_ACP, 0, pChar, charLen, pWChar, len); //多字节编码转换成宽字节编码  pWChar[len] = '\0';//将wchar_t数组转换为CString  CString str;str.Append(pWChar);return str;}char * CStringTopchar(CString str){int n = str.GetLength();int len = WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), NULL, 0, NULL, NULL);//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小char * buf = new char[len + 1];   //以字节为单//宽字节编码转换成多字节编码WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), buf, len, NULL, NULL);buf[len] = '\0';   //多字节字符以'\0'结束return buf;}


多字节编码下:char *转CString可以直接用Format,

如:char *s="han" ;
CString str=_T("han");
str.Format(_T("%s"), s);

2)CString常用的成员函数:

诸如:GetLength,Find,Left,Right,Mid,FindOneOf,Insert,Delete,MakeUpper,MakeLower之类的,CString功能非常强大,只要是关于字符串的操作,CString基本都可以很轻松的完成,具体使用参见

另附,一些前辈写过的一些函数,用于不同编码格式的转换,至于编码ANSI,UNICODE,GB2312,GBK,UTF

-8等等,百度区别~:


wstring ANSIToUnicode( const string &str ){int unicodeLen = MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,NULL,0 ); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen]; wmemset(pUnicode,0,unicodeLen); MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,pUnicode,unicodeLen ); wstring rt;rt = wstring(pUnicode);delete pUnicode; return rt; }string UnicodeToANSI( const wstring &str ){char*     pElementText;int    iTextLen;iTextLen = WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL );pElementText = new char[iTextLen];memset(pElementText, 0, iTextLen );WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,pElementText,iTextLen,NULL,NULL );string strText(pElementText);delete[] pElementText;return strText;}std::string UnicodeToUtf8(const std::wstring& widestring)  {  int utf8size = WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  char *buf = new char[utf8size];memset(buf,0,utf8size);WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, buf, utf8size, NULL, NULL);  string str(buf);delete buf;return str;  }  std::wstring Utf8ToUnicode( const string & Utf8string ){int unicodeSize = MultiByteToWideChar(CP_UTF8,0,Utf8string.c_str(),-1,NULL,0);wchar_t *buf = new wchar_t[unicodeSize];wmemset(buf,0,unicodeSize);MultiByteToWideChar(CP_UTF8,0,Utf8string.c_str(),-1,buf,unicodeSize);wstring wstr(buf);delete buf;return wstr;}std::string AnsiToUtf8(std::string  AnsiString){//首先Ansi转为Unicodestd::wstring Unicodestring = ANSIToUnicode(AnsiString);//然后Unicode转UTF8std::string Utf8String = UnicodeToUtf8(Unicodestring);return Utf8String;}std::string Utf8ToAnsi(std::string Utf8String){std::wstring UnicodeString = Utf8ToUnicode(Utf8String);std::string AnsiString = UnicodeToANSI(UnicodeString);return AnsiString;}


0 0