Type conversion-How can I convert cstring data type to char* 
Author Message
 Type conversion-How can I convert cstring data type to char*

I can convert char* data type to cstirng sucessfully.
(like this,char *z = "initial";CString switchletter =z;)
But I did not know how to convert cstring to char*.

I intend to pass char data type in different class.
Can the cstring data type be passed between different
classes?If it did,I will use cstring instead of char.

I did really search all the related on-line document,
but I still cannot find the answer for it.

thanks for your kind help on this.
ps.How to convert int data type to char or cstring?



Fri, 27 May 2005 02:17:06 GMT  
 Type conversion-How can I convert cstring data type to char*
CString has an operator (LPCTSTR) that will automatically get you  const
char* from a CString.  This operator usually makes it unnecessary to do much
else.  For example, the following should work automagically:

char buffer[100];
CString s = "Whatever";
strcpy(buffer, s);

If you need non-const access to the CString buffer, take a look at
CString::GetBuffer() and CString::ReleaseBuffer().  Read the docs carefully!
There's a couple potential "gotchas".

You can pass CString objects.  (Is there anything you can't pass with
c++?!?)  As with many c++ objects, you should carefully consider passing
references (particularly const references) when appropriate.

As for your conversion question:

int i = 1234;    // a generic integer
CString s;    // a CString
s.Format("%d", i);    // converts integer to CString

For char data type, itoa(...), sprintf(...), etc.

TFM3

Note: Spam-resistant e-mail address


Quote:

> I can convert char* data type to cstirng sucessfully.
> (like this,char *z = "initial";CString switchletter =z;)
> But I did not know how to convert cstring to char*.

> I intend to pass char data type in different class.
> Can the cstring data type be passed between different
> classes?If it did,I will use cstring instead of char.

> I did really search all the related on-line document,
> but I still cannot find the answer for it.

> thanks for your kind help on this.
> ps.How to convert int data type to char or cstring?



Fri, 27 May 2005 03:09:15 GMT  
 Type conversion-How can I convert cstring data type to char*

Quote:

> I can convert char* data type to cstirng sucessfully.
> (like this,char *z = "initial";CString switchletter =z;)
> But I did not know how to convert cstring to char*.

> I intend to pass char data type in different class.
> Can the cstring data type be passed between different
> classes?If it did,I will use cstring instead of char.

> I did really search all the related on-line document,
> but I still cannot find the answer for it.

> thanks for your kind help on this.
> ps.How to convert int data type to char or cstring?

95% of the time you should pass strings to functions by declaring the
function parameter as type LPCTSTR.

void Func(LPCTSTR s)
{

Quote:
}

This function will accept a CString very efficiently and it will also
accept a char*.  Almost all of the MFC and C lib and Win32 functions do
this so you can pass either kind of string to them.

The other 5% of the time (when the function must modify the caller's
string) you must pass a CString* or CString& parameter, or pass the
char* that you can get from CString::GetBuffer.  When you use GetBuffer
you also have to call ReleaseBuffer later.

--
Scott McPhillips [VC++ MVP]



Fri, 27 May 2005 03:42:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. (Multiple) converting type list char/CString/char[]

2. (Multiple) converting type list char/CString/char[]

3. CString, char and other Conversion from other types

4. Type conversions from CString or char[] to int

5. HOW to Convert BSTR Type to CString type

6. convert CString to char type

7. How to convert char type to binary type

8. How to convert char type to binary type

9. write CString data into a field in MS sql server in char type

10. convert CString into other numeral data type

11. converting BSTR data type to common string type!

12. Getting the data from a CString into an LPVOID data type

 

 
Powered by phpBB® Forum Software