CString、TCHAR、char转换

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

  1. CString StrInfo="C:\\Windows";   
  2. LPTSTR StrTest=StrInfo.GetBuffer(20);   
  3. strcpy(StrTest,"NewString");   
  4. StrInfo.ReleaseBuffer();   
  5.   
  6. MessageBox(StrInfo,"信息提示",MB_OK);   

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

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

  1. CString str("CString");   
  2.  TCHAR* szMsg = new TCHAR[100];   
  3.  //其参数为CString字符串的长度   
  4.  szMsg = str.GetBuffer(str.GetLength());   
  5.  str.ReleaseBuffer();   
  6.  delete []szMsg;   
  7.  szMsg = NULL;   

TCHAR*->CString的转化

  1. TCHAR szTchar[18] = L"TCHAR";      
  2.  CString  str;      
  3.  str.Format(_T("%s"),szTchar);    
  1. //////////////////////////////    
  2. /*   
  3. ***********************************************************************   
  4. * 函数: TransCStringToTCHAR   
  5. * 描述:将CString 转换为 TCHAR*   
  6. * 日期:2003-05-30 King   
  7. ***********************************************************************   
  8. */    
  9. TCHAR* CPublic::CString2TCHAR(CString &str)    
  10. {    
  11. int iLen = str.GetLength();    
  12. TCHAR* szRs = new TCHAR[iLen];    
  13. lstrcpy(szRs, str.GetBuffer(iLen));    
  14. str.ReleaseBuffer();    
  15. return szRs;    
  16. }    
  17.   
  18. /*   
  19. ***********************************************************************   
  20. * 函数: THCAR2Char   
  21. * 描述:将TCHAR* 转换为 char*   
  22. * 日期:2003-05-30 King   
  23. ***********************************************************************   
  24. */    
  25. char* CPublic::THCAR2char(TCHAR* tchStr)    
  26. {    
  27. int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度    
  28. char* chRtn = new char[iLen+1]    
  29. wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值    
  30. return chRtn;    
  31. }    
  32.   
  33. /*   
  34. CString、TCHAR*、char*转换  
  35.  
  36. ***********************************************************************   
  37. * 函数: CString2char   
  38. * 描述:将CString转换为 char*   
  39. * 日期:2003-05-26 King   
  40. ***********************************************************************   
  41. */    
  42. char* CPublic::CString2char(CString &str)    
  43. {    
  44. int len = str.GetLength();    
  45. char* chRtn = (char*)malloc((len*2+1)*sizeof(char));//CString的长度中汉字算一个长度    
  46. memset(chRtn, 0, 2*len+1);    
  47. USES_CONVERSION;    
  48. strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer()));    
  49. return chRtn;    
  50. }    
  51. //参考    
  52. ///////////////////////    
  53. //Pocket PC上的UNICODE和ANSI字符串    
  54.   
  55. //By Vassili Philippov, September 26, 2001.    
  56.   
  57. //杨方思歧 译    
  58. ////////////////////////    
  59. /*   
  60. ***********************************************************************   
  61. * 函 数 名:GetAnsiString   
  62. * 描 述:将CString(unicode)转换为char*(ANSI)   
  63. * 参 数:CString &s 要转换的CString   
  64. * 返 回 值:返回转换结果   
  65. * 日期、作者:创建日期:2003-5-30 King   
  66. * 最后修改:2003-5-30 King   
  67. ***********************************************************************   
  68. */    
  69. char* GetAnsiString(const CString &s)    
  70. {    
  71. int nSize = 2*s.GetLength();    
  72. char *pAnsiString = new char[nSize+1];    
  73. wcstombs(pAnsiString, s, nSize+1);    
  74. return pAnsiString;    
  75. }    
  76.   
  77. char szA[100];             //An ANSI string buffer   
  78. WCHAR szW[100];            //A Unicode string buffer   
  79.   
  80. //Normal sprintf:all strings are ANSI   
  81. sprintf(szA, "%s","ANSI Str");   
  82.   
  83. //Converts Unicode string to ANSI   
  84. sprintf(szA,"%S",L"Unicode Str");   
  85.   
  86. //Normal swprintf:all strings are Unicode   
  87. swprintf(szW,L"%s",L"Unicode Str");   
  88.   
  89. //Converts ANSI string to Unicode   
  90. swprintf(szW,L"%S""ANSI Str");   
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓