VS2010 CString转为char的方法

VS2010 CString转为char的方法

测试环境:WIN7 64位,VS2010的WIN32控制台下

VS2010 CString转为char的方法 测试环境:WIN7 64位,VS2010的WIN32控制台下
VS2010 CString转为char的方法 测试环境:WIN7 64位,VS2010的WIN32控制台下

包涵头文件

#include <iostream>
#include <stdio.h>
#include <afx.h>
工程属性设置为:
 
封装函数:

函数功能:将多字节字符转为单字符型

参数1:[in][out] pDest 指向目标地址指针,即转换后存放的地址

参数2:[in] pSource 引用原CString对象

int My_WcharToChar(char* pDest,CString& pSource)
{
           
    
 wchar_t* pawstr = NULL;
 pawstr = pSource.GetBuffer(pSource.GetLength()+1);
 wcstombs(pDest,pawstr,pSource.GetLength()+1);
 return   TRUE; 
}

完整测试代码:

#include <iostream>
#include <stdio.h>
#include   <afx.h>
using namespace std;
int My_WcharToChar(char*   pDest,CString& pSource);  int main()
{   CString str = _T("WINDOWS7");
 char   pChar[200];
 My_WcharToChar(pChar,str);
 cout <<"pChar =   "<<pChar<<endl;    return TRUE;
 }  int My_WcharToChar(char* pDest,CString&   pSource)
{
           
    
 wchar_t* pawstr = NULL;
 pawstr =   pSource.GetBuffer(pSource.GetLength()+1);
 wcstombs(pDest,pawstr,pSource.GetLength()+1);
 return   TRUE; 
}

输出结果:

包涵头文件 #include #include #include 工程属性设置为:   封装函数: 函数功能:将多字节字符转为单字符型 参数1:[in][out] pDest 指向目标地址指针,即转换后存放的地址 参数2:[in] pSource 引用原CString对象 int My_WcharToChar(char* pDest,CString& pSource) {  wchar_t* pawstr = NULL;  pawstr = pSource.GetBuffer(pSource.GetLength()+1);  wcstombs(pDest,pawstr,pSource.GetLength()+1);  return TRUE;  } 完整测试代码: #include #include #include using namespace std; int My_WcharToChar(char* pDest,CString& pSource); int main() {  CString str = _T("WINDOWS7");  char pChar[200];  My_WcharToChar(pChar,str);  cout <<"pChar = "<

经验分享 程序员 微信小程序 职场和发展