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