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

来源:互联网 发布:windows 开发周期 编辑:程序博客网 时间:2024/04/28 11:59

 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 转 CStringCString ans;ans.Format("%s", str.c_str());return ans; #endif // _UNICODE  }


3 0