How to convert a CString to a char

Started by
4 comments, last by wall 21 years, 9 months ago
Hi! Does anybody know how to convert a CString to a char? Thanks in advance William D.
Advertisement
A string is a group of characters, a char is ONE character. What exactly are you trying to do?
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.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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
Try looking at GetBuffer()........
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.
Editor42 ...builds worlds

This topic is closed to new replies.

Advertisement