Click to See Complete Forum and Search --> : char arrays to strings
white_as_snow
Oct 13th, 2002, 07:48 PM
i run vc++.net
how do i convert an char array to a string, and vice versa? everything i've tried gives an error, like '= cannot convert from char(2) to char (string?)' and stuff like that.
help puhlease!
HairyDave
Oct 14th, 2002, 03:40 AM
Should be able to use:
string myString = "my string";
char *myCharstar = new char[myString.length()];
strcpy(myCharstar, myString.c_str());
string.c_str() converts a string to a const char*. Then the copy copies the string.
OK?
HD
CornedBee
Oct 14th, 2002, 12:21 PM
The other way round:
const char *cstr = "Hello";
string str;
str = cstr;
white_as_snow
Oct 14th, 2002, 08:36 PM
guess what?
about an hour after i posted (i was offline) i figured it out.
btw, cornedbee, i found that, too, but it gave me a bunch of junk (y->:) ) - or something like that after it. got any suggestions about that?
here's what I had:
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
int _tmain(void)
{
char letters[5];
String* myword;
letters[0] = 'h';
letters[1] = 'e';
letters[2] = 'l';
letters[3] = 'l';
letters[4] = 'o';
myword = letters;
Console::WriteLine(myword);
return 0;
}
Zaei
Oct 14th, 2002, 09:22 PM
char letters[6]
...
letters[5] = 0
Z.
HairyDave
Oct 15th, 2002, 03:14 AM
Sorry, I'm a bit of a retard :rolleyes: Didn't read the post properly. Could have saved everyone a bit of time by shutting up........*slap*.
HD
CornedBee
Oct 15th, 2002, 04:20 AM
Oh, .net String...
We thought C++SL string.
Well, it should work much the same way.
white_as_snow
Oct 15th, 2002, 09:14 AM
well i found out i just needed to add __nogc[] after the variable identifier. now its just giving me grief in getting the string into the array. i suppose it would help if i remembered the suggestions above;)
parksie
Oct 15th, 2002, 09:27 AM
You'd find it easier learning normal C++ before Managed C++ which is a nightmare for normal C++ programmers :D
If you're going to use .NET, use C#. It's not actually a bad language (some bits of it are very neatly designed, IMO) and most of their stuff works with it.
CornedBee
Oct 15th, 2002, 09:38 AM
Unless you have a very special case where you need to mix managed and unmanaged code. C++ is the only .NET language capable of this.
But for 99.9999% of the cases C# is the better choice.
parksie
Oct 15th, 2002, 09:45 AM
C# can do pointers, I believe. You give your function the "unsafe" modifier. Although then you can only call it from another unsafe function :-/
CornedBee
Oct 15th, 2002, 10:28 AM
But only in C++ you can do things like mixing the managed (garbage collected) heap with a standard heap, directly calling WinAPI and such useless stuff.
parksie
Oct 15th, 2002, 10:47 AM
Yeah. Although with .NET you shouldn't really need to. And with Mono you won't be able to... :)
nabeels786
Oct 15th, 2002, 12:06 PM
whats managed/unmanaged c++/whats the diff?
CornedBee
Oct 15th, 2002, 12:07 PM
and such useless stuff.
CornedBee
Oct 15th, 2002, 12:10 PM
Unmanaged C++ is traditional C++ the way we know it.
Managed C++ is a special capability of the Visual C++.NET compiler. It means that C++ code is compiled to .NET MSIML (bytecode) instead of native code, uses the .NET CLR (framework) and GC (garbage collection). Beside the /clr compiler switch it also requires a few special keywords to be used, the most important being __gc for class and/or pointer declarations (a bit tricky). The #import directive and the using keyword are also important, as well as the __delegate keyword.
nabeels786
Oct 16th, 2002, 05:46 PM
so it needs the .net runtimes?
white_as_snow
Oct 16th, 2002, 08:26 PM
well, im in a class that only teaches managed that im already past the drop point, so im not going to waste my money
anyway, let me restate the problem, with the changes that ive discovered. remember, this is c++.net
consider this:
__gc public class someclass
{
public:
String* myMessage;
someclass()
{
char tempMessage __nogc[] = myMessage;
//do stuff to it
//*
myMessage = tempMessage;
}
};
The line after the //* already works. its the first part that doesn't. so far, trying it like that gives me the fewest errors.
btw, that strcopy method didn't work. i don't think the c_str function is there anymore. anyway, that gives me an error at the char array line:
error C2440: 'initializing' : cannot convert from 'System::String __gc *' to 'char __gc[]'
that is where the problem is at now:(
CornedBee
Oct 17th, 2002, 05:31 AM
Try
char tempMessage __nogc[] = myMessage.ToCharArray();
white_as_snow
Oct 18th, 2002, 05:10 PM
I tried that (except now it needs a -> operator instead of a . operator) and it said something like 'cannot convert from _w _char to char __nogc[]'. i don't think that's exactly what it said, but it's close.
CornedBee
Oct 19th, 2002, 08:01 AM
Sorry, don't know any more things...
Oh yeah, you could try copying it to a __gc array and then use strcpy to copy the content of the __gc array to a __nogc array. I admit that's a pretty desperate idea.
white_as_snow
Oct 29th, 2002, 03:48 PM
actually, i got help from a professor and ended up with this:
char myArray[5];
String* myString = "hello";
for(int x = 0; x < =4; ++x)
{
myArray[x] = myString->get_char(x);
}
at least you don't have to worry about it now :D
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.