CString和char *

1. char * 可以直接赋值给CString,如:

char data[5];
CString RecData;

data[0] = 't';
data[0] = 'e';
data[0] = 's';
data[0] = 't';
data[0] = '1';

RecData = data;

MessageBox(RecData);

输出为: test1

 

2. CString转成char *,如:

CString SendData = "test2";
unsigned char * RecData;

int StrNum;
StrNum = SendData.GetLength();

RecData = new unsigned char [StrNum];

RecData = (unsigned char* )(LPCSTR)SendData;


 

则 RecData[0] = 't';

     RecData[0] = 'e';

     RecData[0] = 's';

     RecData[0] = 't';

     RecData[0] = '2';

 

如果像上例知道CString的字符个数,也可以这么写:

CString SendData = "test2";
unsigned char  RecData[5];

RecData = (unsigned char* )(LPCSTR)SendData;

RecData的输出效果一样:

     RecData[0] = 't';

     RecData[0] = 'e';

     RecData[0] = 's';

     RecData[0] = 't';

     RecData[0] = '2';


 

你可能感兴趣的:(CString和char *)