能力值:
( LV2,RANK:10 )
|
-
-
7 楼
..刚做过,强转就行了,滴水练习题,我也在学 char data[]= {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
};
// type: byte num: data
void search(int type, int num)
{
char *start = data;
int length = sizeof(data)/sizeof(data[0]);
int i=0;
while(i< length-type)
{
if(type == 4)
{
if(*(int*)&data[i] == num)
{
printf("address:%x , data:%d\n",&data[i],(int)num);
}
}else if(type == 2)
{
if(*(short*)&data[i] == (short)num)
{
printf("address:%x , data:%d\n",&data[i],(short)num);
}
}else
{
if(*(char*)&data[i] == (char)num)
{
printf("address:%x , data:%d\n",&data[i],(char)num);
}
}
i += 1;
}
}
int main(int argc, char* argv[])
{
printf("data start from:%x\n", data);
search(4,100);
return 0;
} 这是没解耦的,接下来解耦的, 对其进行解耦,接口不改变
char data[]= {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
};
// type: byte num: data
void search_int(int num, int length)
{
int i=0;
while(i<length-4)
{
if(*(int*)&data[i] == num)
{
printf("address:%x , data:%d\n",&data[i],(int)num);
}
i++;
}
}
void search_short(int num, int length)
{
int i=0;
while(i<length-2)
{
if(*(short*)&data[i] == num)
{
printf("address:%x , data:%d\n",&data[i],(short)num);
}
i++;
}
}
void search_char(int num, int length)
{
int i=0;
while(i<length-1)
{
if(*(char*)&data[i] == num)
{
printf("address:%x , data:%d\n",&data[i],(char)num);
}
i++;
}
}
void search(int type, int num)
{
int length = sizeof(data)/sizeof(data[0]);
int i=0;
typedef void (*funcptr)(int,int);
funcptr p;
if(type == 4)
{
p = search_int;
}
else if(type == 2)
{
p = search_short;
}else
{
p = search_char;
}
p(num,length);
}
int main(int argc, char* argv[])
{
printf("data start from:%x\n", data);
search(4,0x64);
return 0;
}
第二题 #include "string.h"
char data[]=
{
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
0x00,0x02,0x57,0x4F,0x57,0x00,0x06,0x08,0x00,0x00,
0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
};
char* FindRoleNameAddr(char* pData,char* pRoleName)
{
int length = strlen(pRoleName);
int all = sizeof(data)/sizeof(pData[0]);
int i=0;
while(i < all -length)
{
if(strncmp(&pData[i],pRoleName,length) == 0)
{
return &pData[i];
break;
}
i++;
}
return NULL;
}
int main(int argc, char* argv[])
{
char *result = FindRoleNameAddr(data,"WOW");
if(result != NULL)
printf("%p\n", result);
else
printf("not found\n");
return 0;
}
|