We help IT Professionals succeed at work.

Getting the char from a CString

thanesh asked
Medium Priority
Last Modified: 2008-02-01
Hi Experts,

I am getting one char from the user.  I have a EditBox control that gets this character in
a CString variable.  I need to check the validity of the character.  I am only getting one character from the user.

CString      m_oneLetter;

I need to convert this to a character and do the validation.  How can I convert it.

Watch Question

CString m_oneLetter("a");
char c = ((LPCTSTR)m_oneLetter)[0];
c contains your one char.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks, but I only get the correct user entered value when I use.

m_oneLetter.GetBuffer(1).  So, how can I change this to a character?
>> m_oneLetter.GetBuffer(1).
char c = m_oneLetter.GetBuffer(1)[0];

Use this its easier
CString m_oneLetter("a");
char c = m_oneLetter.GetAt(0);


Oops.  If I use GetBuffer(1) it returns the character.  But, when I use GetAt(0) ( not 1 as GetLength() returns 0) I am getting an assertion error.
How can I do it then :-)  Thanks.
>>>> So, how can I change this to a character?

char c = m_oneLetter[0];

>>>> m_oneLetter.GetBuffer(1)

You don't need that function. The purpuse of GetBuffer is to get a writeable  char array from CString, e. g.

   char* psz = m_oneLetter.GetBuffer(255);

After that you could copy data to that buffer:

   strcpy(psz, "Hello World");

But  you need to make the CString valid after copying:


After that m_oneLetter is a valid CString again cause it could set it's internal lengths members properly.


   m_oneLetter = "Hello World";

would do the same as the three statements GetBuffer, strcpy, ReleaseBuffer.

Regards, Alex

>>>> If I use GetBuffer(1) it returns the character

No. GetBuffer only returns the internal char array of CString as a writeable (non-const) char pointer.

>>>> when I use GetAt(0) ( not 1 as GetLength() returns 0) I am getting an assertion error.

GetAt(0) gets an assertion if the length of the CString is 0 or undefined. The latter is the case after you called GetBuffer cause you are now responsible for the the buffer. CString would set its internal length to 1 if you call ReleaseBuffer(-1). The -1 argument tells that the terminating zero character was checked.

If you have a edit box associated with a CString object, you should do the following:

   UpdateData(TRUE);  // That copies data from screen (editbox) to the CString member (m_oneLetter);
   if (m_oneLetter.GetLength() > 0)   // check if user entered at least one character
        char c = m_oneLetter[0];    // get the character and store it to a local char

Regards, Alex


Thanks a lot Alex.  Sorry, I have already accepted Rajeev's answer.  I will give you the points in the next question :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.