当前位置:编程学习 > VC++ >>

C++ string与VC++ CString互转

01 //CString to string
02 string CStringToString(const CString &cstr)
03 {
04     string str=cstr.GetBuffer(0);
05     cstr.ReleaseBuffer();
06     return str;
07 }
08  
09 void CStringToStringEx(const CString &cstr, string &str)
10 {
11     str=cstr.GetBuffer(0);
12     cstr.ReleaseBuffer();
13 }
14  
15 //string to CString
16 CString StringToCString(const string &str)
17 {
18     CString cstr=str.c_str();
19     return cstr;
20 }
21  
22 void StringToCStringEx(const string &str, CString &cstr)
23 {
24     cstr=str.c_str();
25 }
26  
27 //CStringA to string
28 string CStringAToString(const CStringA &cstra)
29 {
30     string str=cstra.GetBuffer(0);
31     cstra.ReleaseBuffer();
32     return str;
33 }
34  
35 void CStringAToStringEx(const CStringA &cstra, string &str)
36 {
37     str=cstra.GetBuffer(0);
38     cstra.ReleaseBuffer();
39 }
40  
41 //CStringW to string
42 string CStringWToString(const CStringW &cstrw)
43 {
44     CStringA ctsra(cstrw.GetBuffer(0));
45     cstrw.ReleaseBuffer();
46     string str=cstra.GetBuffer(0);
47     cstra.ReleaseBuffer();
48     return str;
49 }
50  
51 void CStringWToStringEx(const CStringW &cstrw, string &str)
52 {
53     CStringA ctsra(cstrw.GetBuffer(0));
54     cstrw.ReleaseBuffer();
55     str=cstra.GetBuffer(0);
56     cstra.ReleaseBuffer();
57 }

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,