By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,794 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,794 IT Pros & Developers. It's quick & easy.

Convert a char array to a managed system string

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
"Trep" <da**********@gmail.com> writes:
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.
Jul 22 '05 #2

P: n/a
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 :(

Jul 22 '05 #3

P: n/a
Trep wrote:
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());

]

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

P: n/a
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??

Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

P: n/a
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
Jul 22 '05 #7

P: n/a
Trep wrote:
But... I was under the impression that VC++ .NET used managed strings,
which evolved from CStrings???

..NET uses the System::String type of the CLI standard:

http://www.ecma-international.org/pu...s/Ecma-335.htm

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #8

P: n/a
Ioannis Vranos wrote:

Trep wrote:
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);
]

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

P: n/a
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.

Jul 22 '05 #10

P: n/a
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.

Jul 22 '05 #11

P: n/a
Sorry, I meant to write System::String.

Jul 22 '05 #12

P: n/a
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.

Jul 22 '05 #13

P: n/a
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.

Jul 22 '05 #14

P: n/a
JKop wrote:
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>.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #15

P: n/a
Why is this happening? I'm only hitting submit once!

Jul 22 '05 #16

P: n/a
Trep wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #17

P: n/a
Trep wrote:

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.
Jul 22 '05 #18

P: n/a
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 *);

Jul 22 '05 #19

P: n/a
Ioannis Vranos wrote in news:1098283120.961120@athnrd02 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:

news:microsoft.public.dotnet.languages.vc

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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #20

P: n/a
On Wed, 20 Oct 2004 17:47:39 +0300, Ioannis Vranos
<iv*@guesswh.at.grad.com> wrote in comp.lang.c++:
Trep wrote:
But... I was under the impression that VC++ .NET used managed strings,
which evolved from CStrings???

.NET uses the System::String type of the CLI standard:

http://www.ecma-international.org/pu...s/Ecma-335.htm


And it is completely off-topic here, dipstick. Do not provide
off-topic answers to off-topic questions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #21

P: n/a
On Wed, 20 Oct 2004 18:17:18 +0300, Ioannis Vranos
<iv*@guesswh.at.grad.com> wrote in comp.lang.c++:
JKop wrote:
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);
}


This is not C++. There is no 'gcnew' in C++. Stop posting this
off-topic crap here. Go find a 'CLI' group if you like it so much.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #22

P: n/a
Rob Williscroft wrote:
Nope, CLI will *not* be available *everywhere* C++ is so it will not
be part of C++ and so will remain off topic.
Here are some mainstream CLI compliant engines:

..NET framework (Windows): http://msdn.microsoft.com/netframework

Mono (GNU/Linux, Unix, Mac OS X and Windows): http://www.mono-project.com

DotGNU (GNU/Linux, Mac OS X, Windows): http://www.gnu.org/projects/dotgnu
Check the OSes mentioned.

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).

No, C++/CLI is a separate standard of extensions to ISO C++ for taking
advantage of a CLI machine where one is available.

CLI VMs are already becoming widespread.

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

Until such time please redirect to:

news:microsoft.public.dotnet.languages.vc

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

C++/CLI code has binary compatibility even if you compile it under Windows.
C++/CLI is not a standard yet, so how could there be a C++ compiler with
support for it?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #23

P: n/a
Trep wrote:
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 *);
Marshal class is for:

"Provides a collection of methods for allocating unmanaged memory,
copying unmanaged memory blocks, and converting managed to unmanaged
types, as well as other miscellaneous methods used when interacting with
unmanaged code."
You do not want to convert from managed to unmanaged.
Just you the member function String::ToCharArray:

#using <mscorlib.dll>
int main()
{
using namespace System;

String *s= __gc new String("Test");
wchar_t p __gc[] = s->ToCharArray();
}

It returns a managed array of wchar_t.


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

Correct.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #24

P: n/a
Ioannis Vranos wrote in news:1098327645.822082@athnrd02 in
comp.lang.c++:
Rob Williscroft wrote:
Nope, CLI will *not* be available *everywhere* C++ is so it will not
be part of C++ and so will remain off topic.
Here are some mainstream CLI compliant engines:


Doesn't have anything to do with CLI/C++ portablity, The C++ Standard
or the topic of this newsgroup.

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).

No,


No, What ?
C++/CLI is a separate standard of extensions to ISO C++ for taking
advantage of a CLI machine where one is available.
So it *isn't* part of The C++ Standard or the topicality of
this newsgroup.

CLI VMs are already becoming widespread.

Again that has nothing to do with portablity, The C++ Standard or
the topic of this newsgroup.


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

Until such time please redirect to:

news:microsoft.public.dotnet.languages.vc

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

C++/CLI code has binary compatibility even if you compile it under
Windows.


Doesn't make it topical here. I can write all my code in MSWORD and
compile it anywhere, so is MSWORD on topic now ?

C++/CLI is not a standard yet, so how could there be a C++ compiler
with support for it?


More importantly, how can you possibly think its on topic here ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #25

P: n/a
Rob Williscroft wrote:
Doesn't make it topical here. I can write all my code in MSWORD and
compile it anywhere, so is MSWORD on topic now ?

Well, C++/CLI is a standard. Also there is communication between ISO C++
and C++/CLI committees.
Also check a list of C++/CLI participants:
C++/CLI Participants and Timeline

Participants:
• Convener: Tom Plum
• Project Editor: Rex Jaeschke
• Subject Matter Experts: Bjarne Stroustrup, Herb Sutter
• Participants: Dinkumware, EDG, IBM, Microsoft, Plum Hall…
• Independent conformance test suite: Plum Hall
Ecma + ISO process, estimated timeline:
• Oct 1, 2003: Ecma TC39 plenary. Kicked off TG5.
• Nov 21, 2003: Submitted base document to Ecma.
• Dec 2003 – Sep 2004: TG5 meetings (7).
• Dec 2004: Vote on whether to adopt as Ecma standard.
• Q1 2005: If successful, submit for ISO fast-track process.
• Q1 2006: If ready, vote on whether to adopt ISO standard.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #26

P: n/a
Ioannis Vranos wrote:

Rob Williscroft wrote:
Doesn't make it topical here. I can write all my code in MSWORD and
compile it anywhere, so is MSWORD on topic now ?
Well, C++/CLI is a standard.


Oh, come on.
Ada has a standard, Prolog has a standard, Fortran has a standard.
xxx has a standard.

According to you all of them would be topical now.
Also there is communication between ISO C++
and C++/CLI committees.


And?
As for now CLI is not in the C++ standard. End of story.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #27

P: 1
For converting a String to char you use the following code.

char __nogc* myName1 = static_cast<char*>(Marshal::StringToHGlobalAnsi(st rName).ToPointer());
after the above one you will get your char *. once u finished free the memory allocated for the char*, as below.

Marshal::FreeHGlobal((int)myName1);

By marshalling only u can convert ur managed string to unmanaged char.

Regards,
Senthil
Feb 22 '06 #28

This discussion thread is closed

Replies have been disabled for this discussion.