It's probably best to write the data as a CString for various reasons, but if you need to convert a String (m_sString) string to an ASCII character string, something like this might work for you ...
void myclass::Serialize(CArchive & ar) { CHAR* buf; DWORD len; if (ar.IsStoring()) // Writing { len = m_sString.GetLength(); // Instead of null terminated string, store size. ar << len; buf = (CHAR*)malloc(len); WideCharToMultiByte(CP_UTF8, 0, m_sString, len, buf, len, NULL, NULL); // Convert wide to single bytes ar.Write(buf, len); // Write ascii chars free(buf); } else // Reading { ar >> len; buf = (CHAR*)malloc(len); ar.Read(buf, len); // Read ascii string MultiByteToWideChar(CP_UTF8, 0, buf, len, m_sString.GetBufferSetLength(len), len); // Convert ascii bytes to CString wide bytes free(buf); } }
source share