|
-
Oct 13th, 2002, 07:48 PM
#1
Thread Starter
Member
char arrays to strings
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!
What does "formatting drive C..." mean?!
-
Oct 14th, 2002, 03:40 AM
#2
Addicted Member
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
-
Oct 14th, 2002, 12:21 PM
#3
The other way round:
Code:
const char *cstr = "Hello";
string str;
str = cstr;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 14th, 2002, 08:36 PM
#4
Thread Starter
Member
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;
}
What does "formatting drive C..." mean?!
-
Oct 14th, 2002, 09:22 PM
#5
Frenzied Member
char letters[6]
...
letters[5] = 0
Z.
-
Oct 15th, 2002, 03:14 AM
#6
Addicted Member
Sorry, I'm a bit of a retard Didn't read the post properly. Could have saved everyone a bit of time by shutting up........*slap*.
HD
-
Oct 15th, 2002, 04:20 AM
#7
Oh, .net String...
We thought C++SL string.
Well, it should work much the same way.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 15th, 2002, 09:14 AM
#8
Thread Starter
Member
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
What does "formatting drive C..." mean?!
-
Oct 15th, 2002, 09:27 AM
#9
Monday Morning Lunatic
You'd find it easier learning normal C++ before Managed C++ which is a nightmare for normal C++ programmers 
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 15th, 2002, 09:38 AM
#10
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 15th, 2002, 09:45 AM
#11
Monday Morning Lunatic
C# can do pointers, I believe. You give your function the "unsafe" modifier. Although then you can only call it from another unsafe function :-/
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 15th, 2002, 10:28 AM
#12
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 15th, 2002, 10:47 AM
#13
Monday Morning Lunatic
Yeah. Although with .NET you shouldn't really need to. And with Mono you won't be able to...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 15th, 2002, 12:06 PM
#14
Fanatic Member
whats managed/unmanaged c++/whats the diff?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Oct 15th, 2002, 12:07 PM
#15
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 15th, 2002, 12:10 PM
#16
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 16th, 2002, 05:46 PM
#17
Fanatic Member
so it needs the .net runtimes?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Oct 16th, 2002, 08:26 PM
#18
Thread Starter
Member
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:
Code:
__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
What does "formatting drive C..." mean?!
-
Oct 17th, 2002, 05:31 AM
#19
Try
Code:
char tempMessage __nogc[] = myMessage.ToCharArray();
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 18th, 2002, 05:10 PM
#20
Thread Starter
Member
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.
What does "formatting drive C..." mean?!
-
Oct 19th, 2002, 08:01 AM
#21
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2002, 04:48 PM
#22
Thread Starter
Member
actually, i got help from a professor and ended up with this:
Code:
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
What does "formatting drive C..." mean?!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|