By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

how convert CString to char *?

P: n/a
Neo
how convert CString to char *?

regards,
Mohammad Omer Nasir
Jun 7 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> how convert CString to char *?

For VC2005
http://msdn2.microsoft.com/en-us/library/awkwbzyc.aspx

For VC2003 you'd do the same. except you won't use the 'safe' runtime
functions.
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
//... modify lpsz as much as you want

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jun 7 '06 #2

P: n/a
You can just call GetBuffer() if you want to access the string directly.

Tom

"Neo" <mo******@hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP02.phx.gbl...
how convert CString to char *?

regards,
Mohammad Omer Nasir

Jun 7 '06 #3

P: n/a
Neo wrote:
how convert CString to char *?

regards,
Mohammad Omer Nasir


Mohammad:

Very possibly, you only need a const char*, in which case you can just do

CString str("Hello world\n");
const char* s = str;

David Wilkinson
Jun 7 '06 #4

P: n/a
> how convert CString to char *?

There are already 3 answers, all good, but depends what you need.

If you need a copy that you want to change, then go with new/_tcscpy [Bruno]
If you need to change the string in place, then GetBuffer() [Tom]
If you need read-only access to the buffer, then use implicit cast [David]
But I would like to add a special case: if the application is Unicode, then
the CString is also Unicode, meaning you don't get char*, but wchar_t*.
In this case David's code will not compile.

If you really need char* even for a Unicode application,
then you need to do a conversion to the (most likely) ANSI code page.
You can use WideCharToMultiByte or the T2A conversion macro.
But you have to make sure this is what you really need, because you will
damage all characters outside the code page used for conversion.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 8 '06 #5

P: n/a
Neo

thanks gays for answer.

regards,
Mohammad Omer Nasir.
"Mihai N." <nm**************@yahoo.com> wrote in message
news:Xn********************@207.46.248.16...
how convert CString to char *?
There are already 3 answers, all good, but depends what you need.

If you need a copy that you want to change, then go with new/_tcscpy

[Bruno] If you need to change the string in place, then GetBuffer() [Tom]
If you need read-only access to the buffer, then use implicit cast [David]
But I would like to add a special case: if the application is Unicode, then the CString is also Unicode, meaning you don't get char*, but wchar_t*.
In this case David's code will not compile.

If you really need char* even for a Unicode application,
then you need to do a conversion to the (most likely) ANSI code page.
You can use WideCharToMultiByte or the T2A conversion macro.
But you have to make sure this is what you really need, because you will
damage all characters outside the code page used for conversion.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Jun 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.