Getting the char from a CString

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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thaneshAuthor Commented:
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);
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

thaneshAuthor Commented:
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

thaneshAuthor Commented:
Thanks a lot Alex.  Sorry, I have already accepted Rajeev's answer.  I will give you the points in the next question :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.