July 16th, 2003, 09:19 AM
Conversion of CString to char in embedded VC++ 3.0
I am trying to convert CString into char,but when i use the following code
int length = mCCContent.GetLength();
cptr = mCCContent.GetBuffer(length);
I get an error saying cannot convert from unsigned short* to char*.
The reason i am doing this is to collect the entire CString and convert each character into interger so i could do encryption of that by adding another ascii value.
Any immediate help will be highly appreciated.
Thank you all in advance
July 16th, 2003, 04:33 PM
Is your program using Unicode characters? If so, that's probably the reason your code snippet doesn't compile.
Do you know about TCHARs? They're an alias for character types used throughout MFC. CStrings store arrays of TCHARs. By default, TCHARs are simply chars. But if your code has the Unicode pre-processor flag set, MFC TCHARs become wide-characters (unsigned shorts). This is probably the case for your program.
You probably don't want cptr to be a char*, but instead a TCHAR*. Then you should be able to have it point at the CString's buffer:
LPTSTR cptr = mCCContent.GetBuffer(length);
LPTSTR is just an alias for TCHAR*. Also, I'd recommend not using GetBuffer if all you want is a read-only character array. GetBuffer() has some side-effects that might be undesirable. Instead, you can use CString's character-array conversion operator like this:
LPCTSTR cptr = (LPCTSTR) mCCContent;
A LPCTSTR is just a const TCHAR*.
Hope that helps.
July 17th, 2003, 08:35 AM
Thank you for prompt help. The purpose of my conversion is for encryption. I have to take each ASCII value of the CString buffer and change the ascii value.. For example if I have HELLO, I need to change H to another ascii value say %. So, I would like to know how to convert this unicode to ascii.
Any help from anyone is most welcome
Thank you in advance