Tag Archive: CString conversion

I faced one situation during my current project. Want to convert one CString data to CByteArray.. One simple method is as follows.

CByteArray objByteArray;
BYTE *pByteArray = (PBYTE)(LPCTSTR)csData.GetBuffer();
ULONGLONG uLength = csData.GetLength();
ULONGLONG uCapacity = uLength * 2;

DWORD dwDataLength = static_cast(uLength);
memcpy(objByteArray.GetData(), pByteArray, dwDataLength);


CString to TCHAR

CString is a very useful MFC class for string management. But, sometimes we want to convert CString data to TCHAR or something.

here is one small code snippet for performing this operation. 😉

CString csData(_T(“”));
LPTSTR szData = csData.GetBuffer();
or like this
CString csTemp( "Sample data" );

LPTSTR lpszData = new TCHAR[csTemp.GetLength()+1];

_tcscpy(lpszData , csTemp);

delete[] lpszData;// don't forget to do this.