Convert a char array to a managed system string

T

Trep

Hi there!

I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.

This is necessary because I have some legcay C code that needs to process
a string taken from a textbox, then I need to re-display the string as the
textbox->Text. I easily found how to convert from system::string to char[]
but I can't figure out how to go the other way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S
 
T

Tim Love

Trep said:
Hi there!
I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.
C++ doesn't have system::string, but on the analogu of std::string
system::string s = the_char_array;
is worth trying.
 
T

Trep

Okaaay... first off, let me just apologize for the atrocious spelling in my
initial post. It's still pretty early here and my brain is a little zapped!
I should have taken the time to run a spell-check.

Sorry :(
 
I

Ioannis Vranos

Trep said:
Hi there!

I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.

This is necessary because I have some legcay C code that needs to process
a string taken from a textbox, then I need to re-display the string as the
textbox->Text. I easily found how to convert from system::string to char[]
but I can't figure out how to go the other way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S



..NET is entirely off topic here, however in the future C++/CLI will be
on topic (but not any system specific libraries of course, like ADO .NET
or whatever).


In any case, if you want to convert an std::string to "char[]" a call to
c.str() member function will suffice.


Example in C++/CLI:


std::string s="Test";

String ^someString= gcnew String(s.c_str());

String otherString(s.c_str());



[

Off topic:

In current "managed extensions":


std::string s="Test";

String *someString= __gc new String(s.c_str());

]
 
T

Trep

But... I was under the impression that VC++ .NET used managed strings,
which evolved from CStrings???

My reason for saying this is that I keep getting tips from the compiler
saying the following when, for example, I try to directly set a .NET
textbox contents to equal the contents of a string:

Cannot convert parameter 1 from 'unsigned char *' to 'System::String __gc
*'

So I searched the web and found how to make a System::String __gc * into a
char * but I can't find how to go the other way!

Maybe I'm missing your point??
 
J

JKop

Trep posted:
Hi there!

I've been having a lot of difficult trying to figure out a way to
convert a terminated char array to a system::string for use in Visual
C++ .NET 2003.

This is necessary because I have some legcay C code that needs to
process a string taken from a textbox, then I need to re-display the
string as the textbox->Text. I easily found how to convert from
system::string to char[] but I can't figure out how to go the other
way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S

I haven't a clue about .NET, nor have I had a peek at the definition of
"system::string", but... were I to guess:

system::string ConvertStringToSysString(char const blah[])
{
return system::string(blah);
}


-JKop
 
I

Ioannis Vranos

Ioannis said:
Hi there!

I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.

This is necessary because I have some legcay C code that needs to process
a string taken from a textbox, then I need to re-display the string as
the
textbox->Text. I easily found how to convert from system::string to
char[]
but I can't figure out how to go the other way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S




.NET is entirely off topic here, however in the future C++/CLI will be
on topic (but not any system specific libraries of course, like ADO .NET
or whatever).


In any case, if you want to convert an std::string to "char[]" a call to
c.str() member function will suffice.



And now I realise you meant a null terminated char array to System::String.


String ^text= gcnew String(array);

[

Off topic:

"Managed extensions":


String *text= __gc new String(array);
]
 
J

JKop

Trep posted:
But... I was under the impression that VC++ .NET used managed strings,
which evolved from CStrings???

My reason for saying this is that I keep getting tips from the compiler
saying the following when, for example, I try to directly set a .NET
textbox contents to equal the contents of a string:

Cannot convert parameter 1 from 'unsigned char *' to 'System::String
__gc *'

So I searched the web and found how to make a System::String __gc *
into a char * but I can't find how to go the other way!

Maybe I'm missing your point??

It wants a *pointer* to a System::String object.

(BTW, C++ is case sensitive, so be careful, is it "system::string" or
"System::String"?)


-JKop
 
T

Trep

Thanks for the quick replies. I'm sorry, I didn't realize that this wasn't
an appropriate group for .NET questions. I appreciate your suggestions and
I'll keep trying.
 
T

Trep

Thanks for the quick replies. I'm sorry, I didn't realize that this wasn't
an appropriate group for .NET questions. I appreciate your suggestions and
I'll keep trying.
 
T

Trep

Wow, I was overwhelmed by all of the support I got, and all so quickly!
Following your suggestions re: the System::String constructor, I think
I've managed to get it to work.

The System::String constructor wants a '__wchar_t *' type and I was giving
it a 'char *' so it seems to have worked to cast the char array as a
'__wchar_t *' within the constructor.
 
I

Ioannis Vranos

JKop said:
Trep posted:

Hi there!

I've been having a lot of difficult trying to figure out a way to
convert a terminated char array to a system::string for use in Visual
C++ .NET 2003.

This is necessary because I have some legcay C code that needs to
process a string taken from a textbox, then I need to re-display the
string as the textbox->Text. I easily found how to convert from
system::string to char[] but I can't figure out how to go the other
way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S


I haven't a clue about .NET, nor have I had a peek at the definition of
"system::string", but... were I to guess:

system::string ConvertStringToSysString(char const blah[])
{
return system::string(blah);
}


Close enough for C++/CLI:


inline System::String ^ConvertStringToSysString(cli::array<wchar_t> ^blah)
{
return gcnew System::String(blah);
}


Some examples:


int main()
{
using namespace cli;
using namespace System;

// ISO C++ native wchar_t array
wchar_t text1[] = L"Text1";


// Managed built in array on the managed heap
array<wchar_t> ^text2 = {'T','e','x','t'};


String t1=text1;

String t2=text2;

String t3="Something";


String ^t4= gcnew String(text1);

String ^t5= gcnew String(text2);

}



I am not much fluent in C++/CLI so there may be a better way to do the
assignment in the array<wchar_t>.
 
T

Trep

Wow, I was overwhelmed by all of the support I got, and all so quickly!
Following your suggestions re: the System::String constructor, I think
I've managed to get it to work.

The System::String constructor wants a '__wchar_t *' type and I was giving
it a 'char *' so it seems to have worked to cast the char array as a
'__wchar_t *' within the constructor.
 
I

Ioannis Vranos

Trep said:
Wow, I was overwhelmed by all of the support I got, and all so quickly!
Following your suggestions re: the System::String constructor, I think
I've managed to get it to work.

The System::String constructor wants a '__wchar_t *' type and I was giving
it a 'char *' so it seems to have worked to cast the char array as a
'__wchar_t *' within the constructor.



Not the appropriate approach. To avoid confusing you I will talk to you
with "managed extensions" (although this will be the confusing thing in
the future). :)


System::String provides constructors both both wchar_t * and char *.


#using <mscorlib.dll>


int main()
{
using namespace System;

const char *p1 = "Text1";

const wchar_t *p2 = L"Text2";

String *pstr1= __gc new String(p1);

String *pstr2= __gc new String(p2);
}




String *p= __gc new String(p);


is the conversion you want.
 
J

Julie

Trep said:
Hi there!

I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.

This is necessary because I have some legcay C code that needs to process
a string taken from a textbox, then I need to re-display the string as the
textbox->Text. I easily found how to convert from system::string to char[]
but I can't figure out how to go the other way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S

Repost on: microsoft.public.dotnet.languages.vc and you will get a correct
response to your question.
 
T

Trep

Ahhh I understand completely now! I started tunning into lots of problems
with my first "solution" anyway, because of the obvious differences
between wide chars and chars.

The main confusion in my code was that I was using a buffer of type
unsigned char, and since there is no String constructor for unsigned
chars, it was erroring and telling me it needed a wchar.

In any case, changing my buffer to use chars (there was no reason that I
required unsigned chars) was the easiest solution, and it allowed me to
use the constructor that you instructed.

Thanks again for all of your help!

And just for reference, I used the following:

To convert from a System::String to a char[]:
char * charArray = (char *)(void
*)Marshal::StringToHGlobalAnsi(System::String __gc *);

And to convert from a char[] to a System::String I used:
String *newString = __gc new String(char *);
 
R

Rob Williscroft

Ioannis Vranos wrote in in comp.lang.c++:
.NET is entirely off topic here, however in the future C++/CLI will be
on topic (but not any system specific libraries of course, like ADO ..NET
or whatever).

Nope, CLI will *not* be available *everywhere* C++ is so it will not
be part of C++ and so will remain off topic.

You can make a similar argument about extension's to g++ which are
currently far more portable (*) then CLI extension's, and are likely to
remain so. You can extend this argument to a large number of GNU
libraries (as if this counter needs to be made any more ridiculous :).

*) This is a *massive* under statement as they are actually available.

Or Qt ...

Or Borlands VCL ... (They did get it working on two platform's).

Only thing's that are part of the The C++ Standard are on topic and
*nobody* (except perhaps you) is considering incorporating CLI/C++
into The C++ Standard (future or current).

If you want a (non-Microsoft) newsgroup to discuss CLI/C++ I suggest
you investigate setting one up.

Until such time please redirect to:


They're currently the only vendor with a CLI/C++ compiler and
even that hasen't been released yet.

Its the *best* place for posters to get correct responses
to there CLI/C++ question's, as its where the experts are.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top