I am a fresh Visual C++ .NET Developer. Convert char[1024] to System::String

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

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 ;


}
 
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') ) ;

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());
 
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.
 
Back
Top