{"id":619,"date":"2016-02-19T16:50:57","date_gmt":"2016-02-19T08:50:57","guid":{"rendered":"http:\/\/gnonstop.linkpc.net\/~gordon\/?p=619"},"modified":"2025-08-22T21:55:41","modified_gmt":"2025-08-22T13:55:41","slug":"cstring-%e8%88%87-stdstring-stdwstring-%e4%b9%8b%e9%96%93%e7%9a%84%e8%bd%89%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/guerilla.ddns.net\/wp\/?p=619","title":{"rendered":"CString \u8207 std::string std::wstring \u4e4b\u9593\u7684\u8f49\u63db"},"content":{"rendered":"<h3>CString to std::string<\/h3>\n<p>\u7c21\u55ae\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">CString cs(&quot;Hello&quot;);\nstd::string s((LPCTSTR)cs);<\/code><\/pre>\n<p>&nbsp;<br \/>\n<strong>\u4f46\u662f<\/strong> std::string \u4e26\u975e\u7e3d\u662f\u80fd\u7528 LPCTSTR \u6b63\u78ba\u7684\u5efa\u69cb\u51fa\u4f86\u3002\u4f8b\u5982, \u4ee5\u4e0a\u7684\u7a0b\u5f0f\u78bc\u5728 UNICODE \u7d44\u614b\u4e0b build \u6703\u5931\u6557<br \/>\n&nbsp;<br \/>\n\u7531\u65bc std::string \u7684\u6a19\u6e96\u5efa\u69cb\u5143\u70ba LPSTR \/ LPCSTR, \u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u8981\u4e0d\u5c31\u662f\u7528 VC++ 7.x \u4ee5\u4e0a\u7684\u7248\u672c, \u6216\u662f\u4f7f\u7528\u8f49\u63db\u7528\u7684 classes \u50cf\u662f CT2CA<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">CString cs(&quot;Hello&quot;);\n\/\/ Convert a TCHAR string to a LPCSTR\nCT2CA ascii(cs);\n\/\/ construct a std::string using the LPCSTR input\nstd::string strStd(ascii);<\/code><\/pre>\n<p>&nbsp;<br \/>\nor<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">CString str(_T(&quot;Hello, world!&quot;));\nCT2A ascii(str);\nstd::string s(ascii.m_psz);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>std::string to CString<\/h3>\n<pre><code class=\"language-cpp\" data-line=\"\">std::string s(&quot;Hello&quot;);\nCString cs(s.c_str());<\/code><\/pre>\n<p>&nbsp;<br \/>\nps: class CStringT \u80fd\u5920\u5efa\u69cb\u4f86\u81ea character \u6216\u662f wide-character \u7684\u5b57\u4e32, \u800c\u4e14\u4e0d\u7528\u7279\u5225\u5728\u5b57\u4e32\u7684\u5c3e\u7aef\u7279\u5225\u653e\u9032 NULL, \u5982 std::string s(&#8220;Hello\\0&#8221;);<br \/>\n&nbsp;<\/p>\n<p>&nbsp;<br \/>\nref: <a href=\"http:\/\/stackoverflow.com\/questions\/258050\/how-to-convert-cstring-and-stdstring-stdwstring-to-each-other\" target=\"_blank\" rel=\"noopener\">How to convert CString and ::std::string ::std::wstring to each other?<\/a><\/p>\n<p>&nbsp;<br \/>\n#unicode #ansi #convert #mfc<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CString to std::string \u7c21\u55ae\u65b9\u5f0f\uff1a CString cs(&quot;Hello&#038;quo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212],"tags":[85,27],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-coding","tag-c","tag-coding"],"_links":{"self":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=619"}],"version-history":[{"count":15,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/619\/revisions"}],"predecessor-version":[{"id":3412,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/619\/revisions\/3412"}],"wp:attachment":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}