472,958 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

how convert CString to char *?

Neo
how convert CString to char *?

regards,
Mohammad Omer Nasir
Jun 7 '06 #1
5 5311
> 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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
0
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
1
by: joye | last post by:
Hello , I meet some data convertion problem, the sample code as following, namespace Configure { using namespace System ; typedef struct _SW{ char szVersion ;
1
by: Steven Woody | last post by:
somewhere in my code, char* rtnStrs = { "ok", "error", NULL }; foo( rtnStrs ); here, foo was decleared as 'void foo( const char * rtnStrs ). but the gcc compiler will report ' can not...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
3
by: codercoder | last post by:
It is a C++ questions: How to Convert unsigned char to CString and char *; and backward as well?
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
2
by: Frnk Carleo | last post by:
I am looking to convert a char array defined as char pstrInputClaim to a std::wstring trying to use the constructor as below std::wstring strInputClaim(pstrInputClaim);
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.