Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CString to TCHAR*,int,long

52 views
Skip to first unread message

Scott McPhillips

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to
Florin Timariu wrote:
>
> how do i get char* from a CString.how do i form a CString when i have a
> char* ? how about when CString represents a number? how do i convert it to
> int,or long.I tried casting but it didn't work for me.
>
> Best regards

CString str = "Florin"; // CString from char*

char * pcz = str.GetBuffer(); // char* from CString
str.ReleaseBuffer();

CString str2;
str2 = pcz; // CString from char*

CString str3 = "123";
int i = atoi(str3); // text number to int
long ln = atol(str3); // text number to long

But, instead of a char*, quite often you only need a (const char*) from a CString, and in those cases you can use the CString directly because it has a built in (LPCTSTR) operator. That is why atoi(str3) works.

Best Regards to you!

Florin Timariu

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to

Dan East

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
It depends on whether or not you are creating a unicode app. You
mention TCHAR in the subject of the post. TCHAR maps to either
char or WCHAR depending on whether or not UNICODE is defined.
Unicode strings are not stored as char arrays, but as arrays of
WCHARs. WCHARs are two bytes long. Are you compiling a
unicode app (for NT or CE)? If not, then the advice offered by Scott
McPhillips is what you need...

Dan

Florin Timariu wrote in message ...

Ajay Kalra

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
This will not work in UNICODE. It is only for MBCS build (or a build which
has neither UNICODE or MBCS defined).

GetBuffer() does not return char*, it returns LPTSTR which is char * only in
MBCS.

If it is UNICODE, you would can use macros like T2A (TCHAR to ASCII).

In addition, you dont want to call GetBuffer(), if you dont want to change
the contents of CString. Operator LPCTSTR is sufficient. Instead of atoi()
use _ttoi(), which would be good in all builds.


Ajay


Scott McPhillips wrote in message <36BF94AC...@REMOVEhome.com>...


>Florin Timariu wrote:
>>
>> how do i get char* from a CString.how do i form a CString when i have a
>> char* ? how about when CString represents a number? how do i convert it
to
>> int,or long.I tried casting but it didn't work for me.
>>
>> Best regards
>

0 new messages