How to convert basic::string as a char * ?

How to convert basic::string as a char * ?

Post by dave » Thu, 28 Oct 1999 04:00:00



I like using STL basic::string but sometimes I need to use a char * or a
standard c char array. I s there a way to convert convert a basic::string
into a char * or char[] ?

Thanks

 
 
 

How to convert basic::string as a char * ?

Post by Dan Nguye » Thu, 28 Oct 1999 04:00:00


: I like using STL basic::string but sometimes I need to use a char * or a
: standard c char array. I s there a way to convert convert a basic::string
: into a char * or char[] ?

Do you mean string?

#include <string>
string my_string;

//??

To get a c type string from the string class, use the c_str() member function.

--
       Dan Nguyen          | It is with true love as it is with ghosts;


            25 2F 99 19 6C C9 19 D6  1B 9F F1 E0 E9 10 4C 16

 
 
 

How to convert basic::string as a char * ?

Post by Chris Te » Thu, 28 Oct 1999 04:00:00


string myString;
myString="Something";
char* cStr;
cStr= new char[myString.size()];
strcpy(cStr,myString.c_str());
delete cStr;

>I like using STL basic::string but sometimes I need to use a char * or a
>standard c char array. I s there a way to convert convert a basic::string
>into a char * or char[] ?

>Thanks

 
 
 

How to convert basic::string as a char * ?

Post by Cy Edmund » Thu, 28 Oct 1999 04:00:00


I suggest the following modifications:

string myString;
myString="Something";
char* cStr;
cStr= new char[myString.size()] + 1; // string::size() doesn't report the
trailing 0
strcpy(cStr,myString.c_str());
delete [] cStr; // use array form of delete with array form of new

--
Cy Edmunds


>string myString;
>myString="Something";
>char* cStr;
>cStr= new char[myString.size()];
>strcpy(cStr,myString.c_str());
>delete cStr;


>>I like using STL basic::string but sometimes I need to use a char * or a
>>standard c char array. I s there a way to convert convert a basic::string
>>into a char * or char[] ?

>>Thanks

 
 
 

How to convert basic::string as a char * ?

Post by Puduhai Sreera » Fri, 29 Oct 1999 04:00:00



> string myString;
> myString="Something";
> char* cStr;
> cStr= new char[myString.size()];
> strcpy(cStr,myString.c_str());
> delete cStr;

Isn't char * a null terminated ('\0') string and hence
require 1 space more than the size of myString?
 
 
 

1. Convert a string containing char* to a string containing the hex representation of the same

Basically I want to write a function like:

using namespace std;

void dump(const string& from_string, string& to_string)
{
// convert all characters from the string from_string to a
// hex representation of those characters, separated by space.
// so if from_string = "Hello"
// the to_string should be = "48 65 6c 6c 6f"

I cannot think of any way using STL to achieve this. The only thing
that comes to mind is to use sprintf( %02x ).....kind of C-like code.

thanks
....Abhi

2. msdn sample code not working??? pls help!

3. write std:string to file and convert std::string to char*

4. Teac 532S DAE

5. Converting string(s) (not String) to char *

6. I need a font!

7. const char *p = "string" or const char p[] = "string"?

8. FRAM: Inputs, Anyone?

9. conversion from unsigned char string to char string

10. Convert 32-bit int to string or char-array?

11. Converting to cmdline paramter to string (char *)?

12. Functions to create/convert to unsigned char strings???

13. Can I convert an STL string to a char* ??