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);
objByteArray.RemoveAll();
objByteArray.SetSize(static_cast(dwDataLength));
memcpy(objByteArray.GetData(), pByteArray, dwDataLength);
csData.ReleaseBuffer();

 

Advertisements

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();
csData.ReleaseBuffer();
🙂
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.