Conversion from CString to Char * ??
-
Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks
Today is a gift, that's why it is called the present.
-
Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks
Today is a gift, that's why it is called the present.
Use
CString::GetBuffer()
Nobody can give you wiser advice than yourself. - Cicero
-
Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks
Today is a gift, that's why it is called the present.
A conversion like
char * buf = (char *)(LPCTSTR)m_strAddBlob;
is not correct in case of Unicode strings -- the result looks like a string consisting of the first character only. I think you first have to convert your string from Unicode to Ansi. In new MFC this can be done like this:
CStringA ansi(m_strAddBlob);
Then if you actually need a pointer to constant string, then you can use explicit cast:
const char * buff = ansi;
If you realy need a pointer to non-constant string and modify the string, then use
GetBuffer
andReleaseBuffer
functions:char * buff = ansi.GetBuffer(); . . . ansi.ReleaseBuffer();
I hope this helps.