Archived

This topic is now archived and is closed to further replies.

How to convert a CString to a char

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest Anonymous Poster
A string is a group of characters, a char is ONE character. What exactly are you trying to do?

Share this post


Link to post
Share on other sites
To get a specific character from the CString you could use the GetAt-method or the []-operator.

To treat the CString as a const char*, just cast it to one. E.g. CString str = "asdf"; const char* sz = (const char*) str;

To convert the string "123" to the number 123 use the atoi-function.

Share this post


Link to post
Share on other sites
I don''t know about CString''s but with regular STL string''s you can call string.c_str() to get the C-style string out of it.

string s="Hello";
const char* str = s.c_str();

that will give you an array of char''s.


-David

Share this post


Link to post
Share on other sites
Use the convertion operator LPCTSTR

For instance
int MyInt = atoi((LPCTSTR)MyCString);

operator LPCTSTR gets a const char* buffer.
(LPCTSTR) is implied on most calls requiring a const char*.

If you need a char * (one that you can write to) to modify it, use GetBuffer and ReleaseBuffer if I remember correctly.

Share this post


Link to post
Share on other sites