Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ Strings
#8
This is C++/CLI, not C++.

A CLR string is not a C++ std:Confusedtring. 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:Confusedtring).

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?
Reply


Messages In This Thread
C++ Strings - by Hazzy - 2010-08-23, 10:53 PM
C++ Strings - by Fiel - 2010-08-23, 11:01 PM
C++ Strings - by Russt - 2010-08-23, 11:28 PM
C++ Strings - by Hazzy - 2010-08-23, 11:42 PM
C++ Strings - by Fiel - 2010-08-23, 11:46 PM
C++ Strings - by Hazzy - 2010-08-23, 11:49 PM
C++ Strings - by Russt - 2010-08-23, 11:52 PM
C++ Strings - by Spaz - 2010-08-24, 08:45 PM
C++ Strings - by Hazzy - 2010-08-24, 08:57 PM
C++ Strings - by Kortestanov - 2010-08-25, 05:45 PM
C++ Strings - by IImaplers - 2010-08-26, 02:30 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)