Search This Blog

Saturday, July 17, 2010

Convert CString to std::string and vice versa

// convert from std:string to CString
CString errorMsg'
std::string encodeError = "Error 01";
errorMsg = CString(encodeError.c_str());

// convert from CString to std:string
CString csData = "f1db14dfa5d54f0b";
std::string ssData((LPCTSTR)csData);

Convert CString to char array

Again, when use CString, remember to include afxwin.h

char csRoom [20];
BSTR sRoom = "102";

CString myString1(sRoom);

now, value from CString copied to char array

Convert BSTR to CString

When use CString, remember to include afxwin.h in your header.

BSTR sComPortNumber = "COM1";

//declare CString csComPortNumber and convert BSTR cComPortNumber to CString

CString csComPortNumber(sComPortNumber);