#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12

    Question Conversion of CString to char in embedded VC++ 3.0


    Hi,
    I am trying to convert CString into char,but when i use the following code

    char *cptr;
    CString mCCContent;

    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
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    Hi Joe,

    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
    Raghuraman

IMN logo majestic logo threadwatch logo seochat tools logo