WCHAR char CString等常用类型互转

来源:互联网 发布:淘宝客服招聘 可在家 编辑:程序博客网 时间:2024/05/16 17:33

1、CString to WCHAR*:

[cpp] view plaincopy
  1. WCHAR *wch = (WCHAR*)str.GetBuffer(str.GetLength());  

str为CString类型。


2、WCHRA* to char*:

[cpp] view plaincopy
  1. memset(buf, 0, bufInLen);  
  2.     // WCHRA to char  
  3.     WideCharToMultiByte( CP_ACP, 0, wch, -1, buf, bufInLen, NULL, NULL );  
其中的buf为char类型字符串,bufInLen为buf字符串的长度,wch是最上面的那个WCHAR字符串。

上面两个转换一起使用,可以把中英文的CString字符串转换成ascii编码。


3、char* to WCHAR*:

[cpp] view plaincopy
  1. // char to WCHAR  
  2.     WCHAR* wch = new WCHAR(bufLen);  
  3.     memset(wch, 0, sizeof(wch));  
  4.     MultiByteToWideChar( CP_ACP, 0, buf, bufLen, wch, bufLen/sizeof(WCHAR) );  
其中的buf为char类型字符串,bufInLen为buf字符串的长度。

4、WCHAR* to CString:

[cpp] view plaincopy
  1. // WCHAR to CString  
  2.     for(unsigned int i=0; i<wcslen(wch); i++)  
  3.         str.AppendChar(wch[i]);  
上面的str为CString类型。wch为3那里的WCHAR字符串。
3、4组合起来可以把ascii的字符串转换成中英文的CString。


5、int to char*

[cpp] view plaincopy
  1. char t_id[4] = "";  
  2. char *p = itoa(i, t_id, 10);  
这里i为int类型数据,使用itoa可以将int类型转换成char*类型数据。itoa在stdlib.h头文件里。


6、char* to int 

[cpp] view plaincopy
  1. char str = "123";  
  2. int n = atoi(str);  
将字符串str转换成整数n.
原创粉丝点击