473,372 Members | 1,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Convert a char array to a managed system string

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
27 51624
"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
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
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
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
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
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
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
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
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
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
Sorry, I meant to write System::String.

Jul 22 '05 #12
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
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
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
Why is this happening? I'm only hitting submit once!

Jul 22 '05 #16
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
1
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
5
by: joye | last post by:
Dear All, Many old C standard library is useful for string operation such as strcpy. But I meet some problem, a managed char array which declared in a structure and this structure has a instance...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
0
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error:...
4
by: Peter | last post by:
Does anyone know how to convert the following VB6 code to C# code? Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long) Dim...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.