Archived

This topic is now archived and is closed to further replies.

help: convert "char*" to "String*"

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i already done a search, but found nothing to my needs. i need to convert from the garbage collected "String" to "char*", and vice versa. i managed to make a "String*" to "char*", like this: String*Str = "something"; int L = Str->Length-1; char* NStr = new char [L+1]; for(int i = 0; i <= L; ++i){ *(NStr+i) = (char) Str->get_Chars(i); } *(NStr+L+1)='\0'; but could not convert "char*" to "String*" there is nothing (as much as i know) like: Str->set_Chars(i, *(CHAR+i) ); can anyone help ?!?! note: im using C++ .NET [edited by - zaidgs on April 14, 2004 2:55:30 PM]

Share this post


Link to post
Share on other sites
If you are using the STL template string class, then it provides a function for the string to return a char*.

string.c_str() //I think, just open up and look through it

Share this post


Link to post
Share on other sites
Also, I cannot quite figure out why you do int L = string->Length() - 1 but then add it back on teh very next line.

The STL string class does not include a terminating character so your second line is correct. The first, however, is incorrect.

You can do:

char* str = new char[std::string->length() + 1];
str = std::string->c_str();

Conversely, converting from char* to string* is very easy. Just assign the char* to the new string. The std::string class takes care of the details.

- Jason

Share this post


Link to post
Share on other sites
there is no .c_str() , function !!

i am using:

public __gc __sealed class String : public IComparable,
ICloneable, IConvertible, IEnumerable

if that would help.

in my search i found many talking about
std::string, but thats not what i want.
i want my the string that is garbage collected,
so that i can wrap my c++ class, to be used
with other .NET languages.

[edited by - zaidgs on April 14, 2004 2:54:00 PM]

Share this post


Link to post
Share on other sites

my code is NOT WRONG, its CORRECT.

maybe it just didnt make sense to add one, then subtract one,
but thats why got to my mind when i wrote the function. :D



[edited by - zaidgs on April 14, 2004 3:06:46 PM]

Share this post


Link to post
Share on other sites
I''m not really sure what the big problem is. This compiles just fine for me:

char * some_string = "Moo ha ha";
System::String * str = new System::String(some_string);

Share this post


Link to post
Share on other sites
at first i thought i could use:

char * some_string = "hiiiiii";
String * str;
str = some_string;

but that caused some error.

using your technique gives the same error:
char * some_string = "hiiiiii"
String * str = new String(some_string);

the error is:
i have this VB.NET code:
'--------------------------------------
Label3.Text = x.NewGeneration(TextBox2.Text)
'at this point label3.text = "200000"
'(the value returned by NewGeneration function)
TextBox1.Text = x.GetBestString
'a char* should be converted to String here
'at this point label3.text = "100" !!!!
'(some value, dont know from where it come)
'---------------------------------------

my code obviously uses incorrect memory addresses,
yours (less obviously) has some bug (maybe mine, but
the call is so simple there is no place for errors).

the call is:
String* GetBestString(){
String* NStr = new String( G->GetBestString() );
return NStr;
}


[edited by - zaidgs on April 14, 2004 3:36:26 PM]

Share this post


Link to post
Share on other sites
sorry,
i track back my problem to find that
G->GetBestString() had some problems,
i have fixed everything now.
thx SiCrane, for your good suggestion.

Share this post


Link to post
Share on other sites