MFC的CByteArray、CString类完成动态字节数组使用在C语言中,经常定义一个这样的数组:
[cpp]
view plaincopyunsigned char buf[100] = {0};
但这个数组的长度是固定的,不能动态变化。在MFC中,可以使用CByteArray、CString达到这个目的:
[cpp]
view plaincopy//--------------------------------------------
// 使用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 对无符号字节的存取
//--------------------------------------------
//写入
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的另外一个用法吧。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。