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.

LVL 1
thaneshAsked:
Who is Participating?
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.

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

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?
0
rajeev_devinCommented:
>> 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);
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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.
0
itsmeandnobodyelseCommented:
>>>> 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:

   m_oneLetter.ReleaseBuffer(-1);

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

But:

   m_oneLetter = "Hello World";

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

Regards, Alex

0
itsmeandnobodyelseCommented:
>>>> 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



0
thaneshAuthor Commented:
Thanks a lot Alex.  Sorry, I have already accepted Rajeev's answer.  I will give you the points in the next question :-)
0
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
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.