C++/CLI 中字符串类型的转化: 把System::String转化为std::string
2006-03-26 05:27
531 查看
Stan Lippman's BLog
System::String -> std::string
A reader asks,
Sender: Erik Brendengen
Is there an easy way to convert from String^ to std::string?
Does String ^s = std::string( “bla”).c_str(); work the other way?
This is a FAQ, one that I myself bumped into when I had to pass a System::String retrieved from System::Windows::Forms::TextBox to a native program that expected a std::string.
There are generally two conversion strategies, depending on which side of the managed/native fence you wish to do the conversion work. Here are two methods making use of the PtrToStringChars( String^ ) utility from the vcclr.h header file that is part of Visual C++. Note that these are written in the new C++/CLI syntax – and that these are adapted from an internal FAQ ...
As to the second question, does String^ s = ns.c_str() work? Yes.
Published Wednesday, June 02, 2004 2:56 PM by slippman
Comments
Anonymous comments are disabled
System::String -> std::string
A reader asks,
Sender: Erik Brendengen
Is there an easy way to convert from String^ to std::string?
Does String ^s = std::string( “bla”).c_str(); work the other way?
This is a FAQ, one that I myself bumped into when I had to pass a System::String retrieved from System::Windows::Forms::TextBox to a native program that expected a std::string.
There are generally two conversion strategies, depending on which side of the managed/native fence you wish to do the conversion work. Here are two methods making use of the PtrToStringChars( String^ ) utility from the vcclr.h header file that is part of Visual C++. Note that these are written in the new C++/CLI syntax – and that these are adapted from an internal FAQ ...
As to the second question, does String^ s = ns.c_str() work? Yes.
Published Wednesday, June 02, 2004 2:56 PM by slippman
Comments
David Larsson said: Are there any plans to, ahem, extend std::string to include constructors and extraction methods for String^, like ATL::CString currently has? As a sidenote, this is not the first time I find myself wanting one or two features from CString in std::string, to make the decision for which of the two classes to use for a native Windows C++ application a bit easier... Either way, I'm really looking forward to the new C++/CLI. Actually, the /clr switch works amazingly well even today. June 3, 2004 10:27 AM | |
Anson Tsao [MSFT] said: In Whidbey we'll be supplying much more direct approach to do this type of marshalling via a library. void F( String^ s ) { string s1 = marshal_to<string>( s ); String^ s2 = marshal_to<String^>( s1 ); } If you already have a char*, and want to get a System::String from it, all you have to do is this: void F( const char* s ) { String^ s1 = gcnew String( s ); } June 4, 2004 3:01 PM | |
stan lippman said: as david larsson's ahem indicates, extending the standard ISO-C++ string independent of a standard's body is, i think, unlikely at this point. however, as anson's mail indicates, there is lots of thought being given on how to help users interoperate between the native and managed platforms, and david's request for support in this is not at all unreasonable. June 17, 2004 9:26 AM | |
stan lippman said: Yves Dolce -- the conscience of the blogs -- rightly pointed out that they two conversion samples needlessly prolong the pinning of the internal System::Char array of the String, and has proposed the following preferred alternatives, bool To_CharStar( String^ source, char*& target ) { int len = (( source->Length+1) * 2); target = new char[ len ]; pin_ptr<const wchar_t> wch = PtrToStringChars( source ); return wcstombs( target, wch, len ) != -1; } bool To_string( String^ source, string &target ) { int len = (( source->Length+1) * 2); char *ch = new char[ len ]; bool result ; { pin_ptr<const wchar_t> wch = PtrToStringChars( source ); result = wcstombs( ch, wch, len ) != -1; } target = ch; delete ch; return result; } June 17, 2004 12:16 PM | |
Jonathan Pryor said: In To_string(), shouldn't delete ch; actually be delete[] ch; Since you're allocating an array, you really should use the array deletion expression... July 2, 2004 5:00 AM | |
stan lippman said: you are correct. it is a bad habit for an old dog who was programming with the language before this was added, and of course in current implementations, its absence is actually both non-fatal and possibly more efficient. July 7, 2004 11:39 AM |
相关文章推荐
- C++/CLI 中字符串类型的转化: 把std::string转化为System::String
- 标准字符串std::string和System::String, CString之间的转换
- 类型转化LPCTSTR,std::string,VT_BSTR
- 标准字符串std::string和System::String, CString之间的转换
- 标准字符串std::string和System::String, CString之间的转换
- Android中将List<Map<String, Object>>类型数据与字符串的相互转化
- 将MFC的CString 类型转化为C++标准数据类型std::string
- 再谈如何以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- 泛编程中关于std::string类型字符串长度大于预留空间与小于预留空间之间互相转换的探索测试
- 标准字符串std::string和System::String, CString之间的转换
- Java基础知识强化59:String(字符串)和其他类型的相互转化
- 将科学计数法转化为常规格式再转化为字符串类型(String)
- 再谈怎样以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- 把流转化为字符串String类型的工具类
- 将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- go语言如何将时间转化为字符串String类型
- 删除std::String类型字符串首尾空格
- delphi中如何将string类型的字符串数据转化成byte[]字节数组类型的数据
- String字符串转化为db2的日期类型
- string datdString='Wed Oct 12 2016 00:00:00 GMT+0800 (中国标准时间)'将字符串转化为date类型,格式2016-10-12