数据存储类型之间的转换总结

目录

一、char转 TCHAR

二、TCHAR 转string

三、string转TCHAR

四、TCHAR 转char

五、CString转TCHAR*

六、TCHAR*转CString

七、CString转char

八、CString 转char*

九、char*转CString

十、int 转cstring

十一、枚举类型转换为字符串


一、char转 TCHAR

char a[124];

TCHAR b[124];

MultiByteToWideChar(CP_ACP, 0, a, -1, b, 128);

二、TCHAR 转string

TCHAR *STR=new TCHAR[1200];
std::string TCHAR2STRING(TCHAR *STR)

{
	 int iLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL);
	 char* chRtn =new char[iLen*sizeof(char)];
	 WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
	 std::string str(chRtn);
	 return str;
}

三、string转TCHAR

std::string m_csFileName = std::string("hello");  

TCHAR wc[MAX_PATH];  

#ifdef UNICODE  

 _stprintf_s(wc, MAX_PATH, _T("%S"), m_csFileName.c_str());//%S宽字符  

#else  

  _stprintf_s(wc, MAX_PATH, _T("%s"), m_csFileName.c_str());//%s单字符  

#endif 

四、TCHAR 转char

TCHAR *str=new TCHAR[1200];

LPSTR pszOut = NULL;

int nInputStrLen = wcslen (str);

int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, str, nInputStrLen, NULL, 0, 0, 0) + 2;

pszOut = new char [nOutputStrLen];

memset (pszOut, 0x00, nOutputStrLen);

WideCharToMultiByte(CP_ACP, 0, str, nInputStrLen, pszOut, nOutputStrLen, 0, 0);

fprintf(fp,"%s",pszOut);

五、CString转TCHAR*

CString->TCHAR*的转化可以用函数GetBuff()

函数原型为:LPTSTR GetBuffer( int nMinBufLength );

CString str("CString");

TCHAR* szMsg = new TCHAR[100];

//其参数为CString字符串的长度

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

str.ReleaseBuffer();

delete []szMsg;

szMsg = NULL;

六、TCHAR*转CString

TCHAR szTchar[18] = L"TCHAR";   

CString  str;   

str.Format(_T("%s"),szTchar); 

七、CString转char

CString str=_T("你好");

char ch[4];

for(int i=0;i<str.GetLength();i++)

{

	ch[i]=str.GetAt(i);

}

例:

char str2[120]={0};

for(int i=0;i<pDoc->m_CustomSet->strTime1.GetLength();i++)

{

	str2[i]=pDoc->m_CustomSet->strTime1.GetAt(i);

}

char str3[120]={0};

strcpy(str3,mWYF_V_SENSOR_NODE[i].name);

char *cat=strcat( str3,str2);

八、CString 转char*

CString filePath = openFileDlg.GetPathName();

FILE *fp;

char * pNumber=new char[256];

WideCharToMultiByte(CP_ACP,0,filePath,-1,pNumber,256,NULL,NULL );

fp=fopen(pNumber,"w+");

九、char*转CString

char *str ="aaaa"   

CString  str1(str);  

十、int 转cstring

int i =100;   CString str;   str.Format("%d",i);  //str.Format(_T("%d"),i); 

十一、枚举类型转换为字符串

static inline char* weather_str(enum Enum_weather w) //枚举类型转化为char*

{

	char *strings[] = {"Rainy", "Snowy", "Cloudy", "Sunny",};

	return strings[w];

}

 

 

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页