Numeric type
For basic numeric types, a numeric value that can be mapped directly to a managed type in C + +/CLI can be applied to both managed and unmanaged types, and the compiler automatically transforms it.
basic type |
system Span style= "font-family: Microsoft Jacob Black;" > The corresponding class in the namespace |
note / |
bool |
system::boolean |
bool dirty = false; |
char |
system::sbyte |
char sp = "; |
signed char |
system::sbyte |
signed char ch =-1; |
unsigned char |
system::byte |
unsigned char ch = ' + '; |
wchar_t |
system::char |
wchar_t wch = ch; |
short |
system::int16 |
short s = ch; |
unsigned short |
system::uint16 |
unsigned short s = 0xffff; |
int |
system::int32 |
int ival = s; |
unsigned int |
system::uint32 |
unsigned int ui = 0xFFFFFFFF; |
long |
system::int32 |
|
unsigned long |
system::uint32 |
unsigned long ul = UI; |
long long |
system::int64 |
long long etime = UI; |
unsigned long long |
system::uint64 |
unsigned long long mtime = etime; |
float |
system::single |
float f = 3.14f; |
double |
double d = 3.14159; |
Long double |
System::D ouble |
Long double d = 3.14159L; |
String
The
String CLI has been built in: system:: string , but commonly used strings for C + + are char*, wchar_t*, std:: There are many kinds of string, the compiler provides char*, wchar_t* to System:: Span style= "color: #2b91af;" >string automatic conversion for :
System::String^ s = "Hello worold";
System::String^ s2 = L "Hello worold";
Alternatively, you can use gcnew to create a managed string:
System::string^ s = gcnewstring("Hello worold");
However, for system ::String char*, there is no direct syntax support. There are many ways to do this, and I usually use the following method to convert:
intptr IP = marshal::stringtohglobalansi ( constchar* ch = static_cast<constchar*> (IP. ToPointer ());
//do something with ch
marshal::freehglobal (IP);
There is a need to note that after the conversion of the const char* need to release the conversion process of IntPtr, if there is not much to consider the performance of the place, large can use a std::string copy it, written as follows function form:
#include <string>using namespacestd; using namespaceSystem; using namespacesystem::runtime::interopservices; stringCast_to_string (string^str) {INTPTR IP=Marshal::stringtohglobalansi (str); Const Char* ch = static_cast<Const Char*>(IP. ToPointer ()); stringStdstr =ch; Marshal::freehglobal (IP); returnStdstr; }
Reference article: how to: Marshal ANSI Strings using C + + Interop
Build a bridge between C + + and C + + (iii)--Basic type