Original Post
ok.. I know of memcmp in libc, but I dont THINK it does what I want. what I want is some way of finding the number of bytes that differ between the two blocks for example comparing the strings "hello world" and "Hello World" should return 2, I have created a solution, I''m just not certain that its the optimal solution, its a mess but here it is
the good news is that it hasn''t crashed yet. other than that, I''m not sure if XOR or == is more efficient
int memdiff(void * buf1,void *buf2,int n)
{
int n_int,n_short,n_char,n_diff,r,i,j,k,index;
void *cur1,*cur2;
n_diff=0;
cur1=buf1;
cur2=buf2;
//divide the number of bytes to compare by the size of int
n_int=n/sizeof(int);
//use mod to get the remainder
r=n%sizeof(int);
//get the number of shorts remaining
n_short=r/sizeof(short);
r=r%sizeof(short);
//get the number of chars remaining
n_char=r/sizeof(char);
r=r%sizeof(char);
//test as many ints as we can
for(i=0;i<n_int;i++)
{
//cast to int, if XORing the two ints is NOT 0 we test the
//upper and lower short if those are still different we then
//test upper and lower chars of the given short
if (*(int*)cur1^*(int*)cur2)
{
for(j=0;j<(sizeof(int)/sizeof(short));j++)
{
if(((short*)cur1)[j]^((short*)cur2)[j])
{
for(k=0;k<(sizeof(short)/sizeof(char));k++)
{
index=(sizeof(short)/sizeof(char))*j+k;
if(((char*)cur1)[index]^((char*)cur2)[index])
n_diff++;
}
}
}
}
cur1=((int*)cur1)++;
cur2=((int*)cur2)++;
}
//test as many shorts as there are left
for(i=0;i<n_short;i++)
{
if(*(short*)cur1^*(short*)cur2)
for(j=0;j<(sizeof(short)/sizeof(char));j++)
if(((char*)cur1)[j]^((char*)cur2)[j])
n_diff++;
cur1=((short*)cur1)++;
cur2=((short*)cur2)++;
}
//test as many chars as there are left
for(i=0;i<n_char;i++)
{
if(*(char*)cur1^*(char*)cur2)
n_diff++;
cur1=((char*)cur1)++;
cur2=((char*)cur2)++;
}
return(n_diff);
}