I am a fresh Visual C++ .NET Developer. Can you kindly guide me for
How to Convert char[1024] to System::String
I am using windows forms and trying to set a text value referencing
the method className() of an object of class HetConvert (my own) this
returns a char - which as you can see from the code below I am trying
to pass to label1->Text - but this expects a String^
any clues anyone
Thanks
----------------------
public: System::Void label1_Click(System::Object^ sender,
System::EventArgs^ e) {
HetConvert *hetcnv = new HetConvert ( "Hetcnv" );
char outputString[1024] ;
strcpy(outputString, (hetcnv->className()+ '\0') ) ;
label1->Text= outputString ;
}
William DePalo [MVP VC++] - 21 Feb 2007 01:32 GMT
>I am a fresh Visual C++ .NET Developer. Can you kindly guide me for
> How to Convert char[1024] to System::String
The short answer is that you construct one.
Take a look at this page for more info:
http://blogs.msdn.com/slippman/archive/2004/06/02/147090.aspx
where you will find this
void F( const char* s )
{
String^ s1 = gcnew String( s );
}
Regards,
Will
www.ivrforbeginners.com
Ben Voigt - 21 Feb 2007 14:40 GMT
>I am a fresh Visual C++ .NET Developer. Can you kindly guide me for
> How to Convert char[1024] to System::String
[quoted text clipped - 18 lines]
>
> strcpy(outputString, (hetcnv->className()+ '\0') ) ;
And what do you think that line does? What data type does className()
return? You are adding a character constant zero, if className() is a char*
(which it must be if passed to strcpy), then the '\0' is promoted to
(ssize_t)0, which adds a zero offset to the pointer.
You aren't appending a null character to the string, and you're risking a
buffer overflow and reducing performance for no purpose.
> label1->Text= outputString ;
Get rid of the temporary and use just
label1->Text = gcnew String(hetcnv->className());
> }
Gary - 22 Feb 2007 21:55 GMT
I recommend looking up information on pointers and how strings work in
unmanaged C++. Native string types are memory addresses and adding them
does not append the strings. You can use the Standard Template Library
or MFC string class to make this easier.
> I am a fresh Visual C++ .NET Developer. Can you kindly guide me for
> How to Convert char[1024] to System::String
[quoted text clipped - 22 lines]
>
> }