惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
量子位
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Jina AI
Jina AI
L
LangChain Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学

博客园 - 吴尔平

gtest 的彩色信息输出 + boost.test 的内存泄漏检测及定位 在低版本的 vc 中使用 vc 10.0 的新特性 使用另一个blog: http://blog.csdn.net/WuErPing scons + swig 如何在 vista 使用 Device Emulator 连接internet vc9 Feature Pack Beta tr1 的一些问题 NSIS Kill Process (转贴) C#与一个彩票页面 - 吴尔平 - 博客园 py2exe 转换 pytetris - 吴尔平 关于模板化的friend class C# 的 random shuffle python 读取 windows event log 的简短代码 IronPython 1.0 Release Candidate (转贴 ( 据说比c实现的快1.5!) ) Visual Studio Service Pack (转贴) The 16th Annual Jolt Product Excellence Award Winners (转贴) C++/CLI singleton模式 (双重检测锁实现) 如何在 VS2005 的 Team Unit Testing frameworks 中测试 Native Code (C++ ) 2005 CRT memory leaks 改变 SQL Server 2000 所有对象的所有者
C++/CLI FAQ (逐步整理中)
吴尔平 · 2006-03-20 · via 博客园 - 吴尔平

1.  std::string,std::wstring与System::String^转换

 1  /** \class sidle::StrHelper
 2      \brief std::string,std::wstring与System::String^转换
 3      \author 吴尔平
 4      \version 1.0
 5      \date 2006.03.20 - 
 6      \bug 
 7      \warning
 8  */
 9  
10 namespace sidle
11 {
12     using namespace std;
13     using namespace System;
14     using namespace System::Text;
15 
16     class StrHelper
17     {
18     public:
19         static String^ Str2Refstr(const std::string& src) 
20         {
21             return gcnew String(src.c_str());
22         }
23 
24         static String^ Wstr2Refstr(const std::wstring& src) 
25         {
26             return gcnew String(src.c_str());
27         }
28 
29         static std::wstring Refstr2wstr(String^ src) 
30         {
31             pin_ptr<wchar_t> p = &(src->ToCharArray()[0]);
32             return std::wstring(p);
33         }
34 
35         static std::string Refstr2str(String^ src) 
36         {
37             //cli::array<Byte>^ buf = UnicodeEncoding::Convert(UnicodeEncoding::Unicode, UnicodeEncoding::GetEncoding("gb2312"), 
38             //    Encoding::Unicode->GetBytes(src));
39             cli::array<Byte>^ buf = Encoding::GetEncoding("gb2312")->GetBytes(src);
40             pin_ptr<Byte> p  = &(buf[0]);
41             return std::string((char*)p);
42         }
43     };
44 }
45 
46 void test_RefString_String()
47 {
48     using namespace sidle;
49     assert(StrHelper::Str2Refstr("中文str"== "中文str");
50     assert(StrHelper::Wstr2Refstr(L"中文wstr"== "中文wstr");
51     assert(StrHelper::Refstr2str("中文str"== string("中文str"));
52     assert(StrHelper::Refstr2wstr("中文wstr"== wstring(L"中文wstr"));
53 }