在C语言中,经常定义一个这样的数组:
- unsigned char buf[100] = {0};
但这个数组的长度是固定的,不能动态变化。在MFC中,可以使用CByteArray、CString达到这个目的:
-
-
-
-
- CString buf1;
- buf1 += (unsigned char)0x01;
- buf1 += (unsigned char)0x02;
- buf1 += (unsigned char)0x10;
- buf1 += (unsigned char)0xFF;
-
-
-
- printf("%d\n", buf1.GetLength());
- CString str;
- CString str1;
- for (int i=0; i<buf1.GetLength(); i++)
- {
- unsigned char x = (unsigned char)buf1[i];
- str.Format("%02x ", x);
- str1 += str;
- }
- AfxMessageBox(str1);
-
-
-
-
-
-
- CByteArray arry;
- arry.Add(0x41);
- arry.Add(0x00);
- arry.Add(0xFF);
- arry.Add(0x22);
-
-
- str1.Empty();
- for (i=0; i<arry.GetSize(); i++)
- {
- unsigned char x = (unsigned char)arry[i];
- str.Format("%02x ", x);
- str1 += str;
- }
- AfxMessageBox(str1);
CByteArray用起来比较直观,不说了;使用CString时,不管是读取或者写入,一定注意使用(unsigned char)转型。这也算是CString的另外一个用法吧。