CString和string在unicode与非unicode下的相互转换

 CString和string在unicode与非unicode下的相互转换

最近想写一个手机控制电脑的玩具,涉及到了socket通信,数据采用json通用格式,首先是jsoncpp的编译问题太烦了,然后还有更烦的,java中的String多容易的玩意儿,然后到了c/c++/mfc中超级烦,搜索了很久的攻略,用了大把的时间,最后写了个这玩意儿出来,或许可以帮助到一些需要此的道友们哈

string toString(CString cs) { 
#ifdef _UNICODE
		//如果是unicode工程
		USES_CONVERSION;
		std::string str(W2A(cs));
		return str;
#else
		//如果是多字节工程 
		std::string str(cs.GetBuffer());
		cs.ReleaseBuffer();
		return str; 
#endif // _UNICODE 
	}


	CString toCString(string str) {
#ifdef _UNICODE
		//如果是unicode工程
		USES_CONVERSION; CString s(str.c_str());
		CString ans(str.c_str());
		return ans;
#else
		//如果是多字节工程 
		//string 转 CString
		CString ans;
		ans.Format("%s", str.c_str());
		return ans; 
#endif // _UNICODE  
	}


猜你喜欢

转载自blog.csdn.net/u014303844/article/details/51397556