2010-08-24, 08:45 PM
This is C++/CLI, not C++.
A CLR string is not a C++ std:
tring. You need to convert between them. Here's some code if you don't care that it only works for ASCII (which I assume is the case since you're using a std:
tring).
But why use C++ iostreams when you can use .NET streams?
A CLR string is not a C++ std:
tring. You need to convert between them. Here's some code if you don't care that it only works for ASCII (which I assume is the case since you're using a std:
tring).Code:
//Convert the CLR string to an array of bytes using the ASCII encoding
array<Byte> ^outputChars = System::Text::Encoding::ASCII->GetBytes(Output->Text);
// Pin the array so the garbage collector can't move it while you're using raw pointers to access it. Might crash if Output->Text is empty, you should add a check for that
pin_ptr<Byte> outputCharsPointer = &(outputChars[0]);
// convert the pin_ptr<Byte> to a char* (NOT a C-string because it's not null-terminated!)
char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(outputCharsPointer));
// Convert the char* to a std::string
std::string outputStdString(nativeCharsPointer, outputChars->Length);But why use C++ iostreams when you can use .NET streams?

