在C++.NET中,System::String^与char*相互转换
2012-03-13 11:22
417 查看
在C++.NET中,System::String^与char*相互转换
一个小例:
#include "stdafx.h"
using namespace System; //这个命名空间如果缺的话,IntPtr这个无法识别
int main(array<System::String ^> ^args)
{
char* ch1 = "this is chars ";
//将char*转换为System::String^
String^ str1= System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)ch1);
//System::String^转换为char*
char* ch2 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str1);
Console::WriteLine(str1);
Console::WriteLine(ch2);
Console::ReadLine();
}
一个小例:
#include "stdafx.h"
using namespace System; //这个命名空间如果缺的话,IntPtr这个无法识别
int main(array<System::String ^> ^args)
{
char* ch1 = "this is chars ";
//将char*转换为System::String^
String^ str1= System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)ch1);
//System::String^转换为char*
char* ch2 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str1);
Console::WriteLine(str1);
Console::WriteLine(ch2);
Console::ReadLine();
}
相关文章推荐
- HOW TO:在 Visual C++ .NET 中从 System::String* 转换为 Char*
- Visual C++ .NET 中从 System::String* 转换为 Char*
- 在 Visual C++ .NET 中从 System::String* 转换为 Char*
- 如何在在Visual C++ .NET 中將System::String* 转换为 Char*
- c++ 中 char 与 string 之间的相互转换问题
- C++中string与char*相互转换
- CString,string,char[]的相互转换(vs2010c++验证无误)
- C++中char*\wchar_t*\string\wstring之间的相互转换:
- [ZZ] C++ char*,const char*,string的相互转换
- C++中char*,string,int类型的相互转换
- c++中的char*与wchar_t*与string以及wstring之间的相互转换
- C++中char*\wchar_t*\string\wstring之间的相互转换
- c++ 中 char 与 string 之间的相互转换问题
- C++中string,char,int,double等数据类型的相互转换及与ASCII码的转换
- C++ char*,const char*,string的相互转换
- 【C++ / Java】char数组和string的相互转换及自动转换
- c++ 中 char 与 string 之间的相互转换问题
- C++ char*,const char*,string的相互转换
- C++ char*,const char*,string的相互转换
- c++中char*\wchar_t*\string\wstring之间的相互转换