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!
Printable View
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!
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
The other way round:
Code:const char *cstr = "Hello";
string str;
str = cstr;
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:
Code:#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;
}
char letters[6]
...
letters[5] = 0
Z.
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
Oh, .net String...
We thought C++SL string.
Well, it should work much the same way.
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;)
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.
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.
C# can do pointers, I believe. You give your function the "unsafe" modifier. Although then you can only call it from another unsafe function :-/
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.
Yeah. Although with .NET you shouldn't really need to. And with Mono you won't be able to... :)
whats managed/unmanaged c++/whats the diff?
Quote:
and such useless stuff.
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.
so it needs the .net runtimes?
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:
The line after the //* already works. its the first part that doesn't. so far, trying it like that gives me the fewest errors.Code:__gc public class someclass
{
public:
String* myMessage;
someclass()
{
char tempMessage __nogc[] = myMessage;
//do stuff to it
//*
myMessage = tempMessage;
}
};
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:(
Try
Code:char tempMessage __nogc[] = myMessage.ToCharArray();
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.
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.
actually, i got help from a professor and ended up with this:
at least you don't have to worry about it now :DCode:char myArray[5];
String* myString = "hello";
for(int x = 0; x < =4; ++x)
{
myArray[x] = myString->get_char(x);
}