Unicode下CString与char_转换

来源:互联网 发布:ubuntu c语言 编辑:程序博客网 时间:2024/05/30 05:42

Unicode下CString与char *转换

 

 

在Visual C++.NET2005中,默认的字符集形式是Unicode,但在VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:Multi-Byte Character Set),这样导致在VC6.0中非常简单实用的各类字符操作和函数在VS2005环境下运行时会报各种各样的错误,这里总结了

在Visual C++.NET2005环境中Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。

 

 

(1)、Unicode下CString转换为char * 

方法一:使用API:WideCharToMultiByte进行转换 

CString str = _T("D:\\校内项目\\QQ.bmp"); 

 

//注意:以下n和len的值大小不同,n是按字符计算的,len是按字节计算的 

int n = str.GetLength(); // n = 14, len = 18  

//获取宽字节字符的大小,大小是按字节计算的 

int len = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);  

//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小

 

char * pFileName = new char[len+1];  

//以字节为单位 

//宽字节编码转换成多字节编码 

WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pFileName,len,NULL,NULL); 

 

pFileName[len+1] = '\0'; 

//多字节字符以'\0'结束 

方法二:使用函数:T2A、W2A  

 

CString str = _T("D:\\校内项目\\QQ.bmp");  

//声明标识符

USES_CONVERSION; 

//调用函数,T2A和W2A均支持ATL和MFC中的字符转换

char * pFileName = T2A(str);  

//char * pFileName = W2A(str); //也可实现转换 

注意:有时候可能还需要添加引用

#include <afxpriv.h> 

 

(2)、Unicode下char *转换为CString 

法一:使用API:MultiByteToWideChar进行转换

char * pFileName = "D:\\校内项目\\QQ.bmp";  

//计算char *数组大小,以字节为单位,一个汉字占两个字节 

int charLen = strlen(pFileName);  

//计算多字节字符的大小,按字符计算。

int len = MultiByteToWideChar(CP_ACP,0,pFileName,charLen,NULL,0); 

//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小 

TCHAR *buf = new TCHAR[len + 1]; 

//多字节编码转换成宽字节编码

MultiByteToWideChar(CP_ACP,0,pFileName,charLen,buf,len);  

 buf[len] = '\0'; //添加字符串结尾,注意不是len+1 

 //将TCHAR数组转换为CString 

CString pWideChar;  

pWideChar.Append(buf); 

//删除缓冲区

delete []buf;  

方法二:使用函数:A2T、A2W  

char * pFileName = "D:\\校内项目\\QQ.bmp";  

USES_CONVERSION; 

CString s = A2T(pFileName); 

//CString s = A2W(pFileName); 

 

方法三:使用_T宏,将字符串转换为宽字符  

//多字节字符集,在vc6和vc7种可以编译通过的语句,VS2005不能通过默认为Unicode字符集 

//AfxMessageBox("加载数据失败",0);  

//书写代码使用TEXT("")或_T(""),文本在UNICODE和非UNICODE程序里都通用

AfxMessageBox(_T("加载数据失败"),0);  

 注意:直接转换在基于MBCS的工程可以,但在基于Unicode字符集的工程中直接转换是不可行的,CString会以Unicode的形式来保存数据,强制类型转换只会返回第一个字符。

Unicode下 CString转TCHAR 

TCHAR* szMsg = new TCHAR[wcslen(strAdbCmd)];    

szMsg = strAdbCmd.GetBuffer(strAdbCmd.GetLength()); 

strAdbCmd.ReleaseBuffer(); 

用完了别忘了

delete []szMsg; 

CString转 char* 

      char * charP; 

         CString strP; 

        

WideCharToMultiByte(CP_OEMCP,0,(LPCTSTR)strP,-1,charP,260,0,false); 

char* TCHAR*转 CString 

         

char cstr[20]=”sdaww”;        

TCHAR tcstr[20]=_T(”unicode”); 

         CString str=CString(cstr); 

        CString str1=CString(tcstr); 

    

CString转 int           

CString strNum=_T(“”); 

          int value = _ttoi(static_cast<LPCTSTR>(strNum));            

或者 

          int value = _ttoi(strNum); 

int 转CString 

          int ia=1232;          

CString stri= stri.Format(_T(“%d”),ia);

 

多字节字符集下

 

Cstring转化为char 转化成数组:

 

Cstring   str=”convert”;

 

char char_str [512]; 

strncpy_s(char_str,(LPCTSTR)str_sum,sizeof(char_str));  

转化成char指针:

 

char * ch_a =(char*)str.GetBuffer(str.GetLength());  

char转化为Cstring  

Cstring str; 

char szBuff[2048]; 

str.Format("%s",szBuff); 

CString 转 int :

n=atoi(str);

0 0
原创粉丝点击