473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
27 51728
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::strin g 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 ConvertStringTo SysString(char const blah[])
{
return system::string( blah);
}

Close enough for C++/CLI:
inline System::String ^ConvertStringT oSysString(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.publi c.dotnet.langua ges.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::Stri ngToHGlobalAnsi (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@athnrd0 2 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.l anguages.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
34602
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
10041
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
7568
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. Also, I was wondering could there be issues in not making copies when converting??? I was thinking, when "pointing" to the same data with both managed and unmanaged strings/arrays couldn't there be issues such as the garbage collector trying to...
1
4383
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
4718
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 declared in a__gc class. Sample code as following, namespace Configure { using namespace System ;
8
5910
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 vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
0
3703
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: C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\ cstringt.h(875): error C2664: 'PtrToStringChars' : cannot convert parameter 1 from 'unsigned char *' to 'const System::String __gc *' The code segemnt is:
4
4660
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 sngArray(0) As Single strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65) CopyMemory sngArray(0), ByVal strString, Len(strString) After running the code sngArray(0) = 17.40548
12
13550
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} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.