cstring = string

stephencushen
stephencushen used Ask the Experts™
on
Simple question!

i have a string myString = "01010110";
i need to convert the string into a cstring myCSting

i thought you could say myCString = mystring.
But this throws up a conversion error.

what the best way to get around this error?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
operator= should work with a char*
I performed the following test with success:

char sz[] = "hello";
CString s;
s = sz;

How is myString defined?

Jim

Author

Commented:
the code is as follows

void CMFCCalculatorDlg::OnDecRadio()
{
string binaryValue = decObj.getBinaryValue();
CString m_edit = binaryValue;

UpdateData(FALSE);
}


that was the code that produces the error.

Author

Commented:
the code is as follows

void CMFCCalculatorDlg::OnDecRadio()
{
string binaryValue = decObj.getBinaryValue();
CString m_edit = binaryValue;

UpdateData(FALSE);
}


that was the code that produces the error.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Commented:
I assume string is:

typedef unsigned char* string;

try:
CString m_edit(binaryValue);

Jim

Author

Commented:
no im not using a typedef.
im using the string data type. i include "#include <string>" at the top of the prog.
Commented:
Try the following:

string binaryValue = decObj.getBinaryValue();
CString m_edit = binaryValue.c_str();

Commented:
Or

CString m_edit(binaryValue.c_str());

You need to use the c_str() member function to pass the const char* pointer to CString object.

Commented:
oh, string is from the STL.
Then Axter's solution is the right answer.

I wasn't sure where string came from - sorry.

JB

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial