473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 5346
> 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.