Win32应用程序编程

 

1、要使用CString类型,需包含头文件 #include <atlstr.h>,在MFC中,需包含 #include <afx.h>

  Unicode下CString转成char*:

1 cstring P_Cstring= "";//需要被转化的Cstring
2 int nLength = P_Cstring.GetLength();
3 int nBytes = WideCharToMultiByte(CP_ACP,0,P_String,nLength,NULL,0,NULL,NULL);
4 char* P_char= new char[ nBytes + 1];
5 memset(P_char,0,nLength + 1);
6 WideCharToMultiByte(CP_OEMCP, 0, P_Cstring, nLength, P_char, nBytes, NULL, NULL);

 

2、string、wstring、CString之间的操作

  (1)string是C++提供的标准字符串操作类.wstring是操作宽字符串的类。

       CString是对string(字符串)和wstring(宽字符串)的一个封装,常用在mfc中.用来解决编码问题的。

  (2)string或者wstring转换到CString:

       要把std::string或者std::wstring类型的数据存放到CString中,直接调用string::c_str()或者wstring::c_str()就行了。

        CString转换到string或者wstring

            CString::GetBuffer(0)就能转换到string或者wstring.如果编译时候定义了UNICODE,则转换到wstring;如果未定义,则转换到string。

 

 

 

 

 

1 ////////////////////////////////////////////////////////////////////////////////
2 //Win32
3 ////////////////////////////////////////////////////////////////////////////////
posted @ 2012-11-19 21:18  china_victory  阅读(311)  评论(0编辑  收藏  举报