将CString转换成string ...

=====================此方法在PPC上可编译通过 ,并且正常使用=======================

转自:Siddhartha Rao

// 传统的方法在VS2005的PPC平台上无法编译通过,或者不起作用,要么需要两次转换

//CString->bstr,bstr->string或Cstring->wchar * ->char *等

There are really many ways to do it.

But, the simplest one is just this -

CString strSomeCstring ( "This is a CString Object");

// Use ANSI variant CStringA to convert to char*; construct from it -
std::string strStdString (CStringA (strSomeCstring));


Note that as discussed in this post, CStringA is a template specialization of class CStringT for type char avaílable with Visual Studio 7.x and better.

=======================以下介绍Cstring与其他数据类型的转换========================

转自 :LEO

 

ContractedBlock.gif ExpandedBlockStart.gif Code
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString
//
#include <string>
// 使用CString必须使用MFC,并且不可包含<windows.h>
#define _AFXDLL
#include 
<afx.h>
using namespace std;
//----------------------------------------------------------------------------------
//将 单字节char* 转换为 宽字节 wchar*
inline wchar_t* AnsiToUnicode( const char* szStr )
{
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
if (nLen == 0)
{
   
return NULL;
}
wchar_t
* pResult = new wchar_t[nLen];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, 
-1, pResult, nLen );
return pResult;
}
//----------------------------------------------------------------------------------
// 将 宽字节wchar_t* 转换 单字节char*
inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
   
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 
0, szStr, -1, pResult, nLen, NULL, NULL );
return pResult;
}
//----------------------------------------------------------------------------------
// 将单字符 string 转换为宽字符 wstring
inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr )
{
int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );
wszStr.resize(nLength);
LPWSTR lpwszStr 
= new wchar_t[nLength];
MultiByteToWideChar( CP_ACP, 
0, szStr.c_str(), -1, lpwszStr, nLength );
wszStr 
= lpwszStr;
delete [] lpwszStr;
}
//----------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
char*   pChar = "我喜欢char";
wchar_t
* pWideChar = L"我讨厌wchar_t";
wchar_t   tagWideCharList[
100] ;
char   ch = 'A';
char   tagChar[100= {NULL};
CString   cStr;
std::
string str;
// 注:设置语言环境以便输出WideChar
setlocale(LC_ALL,"chs");

// 注: char* 转换 wchar_t*
// 注: wchar_t 未重载 << ,所以不可使用 cout << 输出
pWideChar = AnsiToUnicode( pChar );
// 注:printf("%ls") 和 wprintf(L"%s") 一致
printf( "%ls\n", pWideChar );

// 注:wchar_t* 转换 wchar_t[]
wcscpy ( tagWideCharList, pWideChar );
wprintf( L
"%s\n", tagWideCharList );

// 注:wchar_t[] 转换 wchar_t*
pWideChar = tagWideCharList;
wprintf( L
"%s\n", pWideChar );

// 注:char 转换 string
str.insert( str.begin(), ch );
cout 
<< str << endl;

// 注:wchar_t* 转换 string
pWideChar = new wchar_t[str.length()];
swprintf( pWideChar, L
"%s", str.c_str());
wprintf( L
"%s\n", pWideChar );

// 注:string 转换 char*
pChar = const_cast<char*>(str.c_str());
cout 
<< pChar << endl;

// 注:char* 转换 string
str = std::string(pChar);
// 注: cout 的 << 重载了string, 若printf 的话必须 printf("%s", str.c_str()); 
//   而不可 print( "%s", str ); 因为 str 是个 string 类
cout << str << endl;

// 注:string 转换 char[]
str = "无聊啊无聊";
strcpy( tagChar, str.c_str() );
printf( 
"%s\n", tagChar );

// 注:string 转换 CString;
cStr = str.c_str();

// 注:CString 转换 string 
str = string(cStr.GetBuffer(cStr.GetLength()));

// 注:char* 转换 CString
cStr = pChar;

// 注:CString 转换 char*
pChar = cStr.GetBuffer( cStr.GetLength() );

// 注:CString 转换 char[]
strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));

// 注:CString 转换 wchar_t*
pWideChar = cStr.AllocSysString();
printf( 
"%ls\n", pWideChar );
}

转载于:https://www.cnblogs.com/Zscorpio/archive/2009/04/14/1435575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随笔 - 764 文章 - 3 评论 - 196 CString,string,char*之间的换() 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差。string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的;char*是从学习C语言开始就已经和我们形影不离的了,有许多API都是以char*作为参数输入的。所以熟练掌握三者之间的换十分必要。 以下我用简单的图示指出三者之间的关系,并以标号对应换的方法。 1 string to CString CString.format("%s",string.c_str()); 2 CString to string string str(CString.GetBuffer(str.GetLength())); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char,CString,sizeof(char)); 6 char * to CString CString.format("%s",char*); CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *换时,要把char定义成为const char*,这样是最安全的。 以上函数UNICODE编码也没问题:unicode下照用,加个_T()宏就行了,像这样子_T("%s") 补充: CString 可能是 CStringW/CStringA,在与 string 换时,如果是 CStringW,还涉及编码换问题。下面以 CStringA 来说明。 1 string to CString CString.format("%s",string.c_str()); CStringA = string.c_str() 就可以了 2 CString to string string str(CString.GetBuffer(str.GetLength())); GetBuffer 有参数的话,可能导致内部的分配空间动作,要进行后续 ReleaseBuffer 操作。 string = CStringA string = CStringA.GetBuffer(); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char *,CString,sizeof(char)); 按照 3 风格,这里应该 char * = CStringA; 或者 char *p = CStringA.GetBuffer(); 6 char * to CString CStringA = char * 就可以了

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值