Convert a String to a char array
-
Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.
-
Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.
#include <vcclr.h> ... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s);
By the way, this is covered in Nish's and my new book on mixing MFC and Managed C++ code. See sig if interested. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson -
#include <vcclr.h> ... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s);
By the way, this is covered in Nish's and my new book on mixing MFC and Managed C++ code. See sig if interested. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardssonor:
__wchar_t __pin * str = &s->ToCharArray()[0];
It's ugly, but a nice hack to convert __gc[] to *. Note: even though the NULL character is not returned by ToCharArray(), the CLR seems to pin it in some "clear" space. :) Any reason why you add theconst
keyword? Doesnt__pin
take care of that? leppie::AllocCPArticle(Generic DFA State Machine for .NET); -
or:
__wchar_t __pin * str = &s->ToCharArray()[0];
It's ugly, but a nice hack to convert __gc[] to *. Note: even though the NULL character is not returned by ToCharArray(), the CLR seems to pin it in some "clear" space. :) Any reason why you add theconst
keyword? Doesnt__pin
take care of that? leppie::AllocCPArticle(Generic DFA State Machine for .NET);The
PtrToStringChars
is more efficient as it simply returns a casted pointer to the string's internal buffer. By comparison, theString::ToCharArray
method creates a brand new copy of the string and returns that to the caller. leppie wrote: Any reason why you add the const keyword? ThePtrToStringChars
function returns a constSystem::Char*
cast of the internal string value. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson -
The
PtrToStringChars
is more efficient as it simply returns a casted pointer to the string's internal buffer. By comparison, theString::ToCharArray
method creates a brand new copy of the string and returns that to the caller. leppie wrote: Any reason why you add the const keyword? ThePtrToStringChars
function returns a constSystem::Char*
cast of the internal string value. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen SigvardssonTom Archer wrote: The PtrToStringChars function returns a const System::Char* cast of the internal string value. But I have never really understood a const parameter, it appears like reverse psychology to me. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
Tom Archer wrote: The PtrToStringChars function returns a const System::Char* cast of the internal string value. But I have never really understood a const parameter, it appears like reverse psychology to me. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
const has nothing to do with __pin. The former simply means that you won't change the value. The latter is used so that the CLR will not move the data in memory. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson -
Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.
Simpler way is to use ATLMFC.
#include <atlstr.h> // CString support. String* strSomething; CString str = strSomething;
But you will need to find a way to pack your ATLMFC7.0 redistributable when you release your app. Hope this helps.