1.转载地址:http://blog.sina.com.cn/s/blog_590be5290100euaq.html
CString转化为CByteArray:
CString str="我的世界 abcdefg";
CByteArray ba;
int nSize = str.GetLength() * sizeof(CHAR);
ba.SetSize(nSize);
lstrcpy((LPTSTR)ba.GetData(),str);
或
CString str="我的世界 abcdefg";
CByteArray ba;
int nSize = str.GetLength() * sizeof(CHAR);ba.SetSize(nSize+1);
memcpy( ba.GetData(), str.GetBuffer(10), nSize );
str.ReleaseBuffer();
再将CByteArray转化为CString:
CString str2('x',nSize);
for(int i=0;i<nSize;i++)
str2.SetAt(i,ba.GetAt(i));
或
CString str2;
char ch;
for(int i=0;i<nSize;i++)
{
ch=ba[i];
str2+=ch;
}
2.转载地址:http://stackoverflow.com/questions/3714964/how-to-convert-from-byte-array-to-cstring-in-mfc
how can I convert a BYTE array in a CString in MFC?
(1)Try this - for eg: - If 'x' is your Byte array then -
BYTE x[5];
x[0] = 'A';
x[1] = 0;
x[2] = 'B';
x[3] = 'C';
x[4] = 0;
CString str( (LPCSTR) &x, sizeof(x) );
(2)
BYTE x[5]; x[0] = 'A'; x[1] = 'B'; x[2] = 'C'; x[3] = 'D'; x[4] = '0';
CString str = TEXT("");
CString s;
for(int i=0;i<5;i++)
{
str.Format(L"%C",x[i]);
s += str;
}
AfxMessageBox(s);