Xor two arrays of varying length

I need to xor two arrays of varying length. This is what I have so far

// EXERCISE 7#
unsigned char a[] = {0x11, 0x22, 0x33, 0x44};
unsigned char b[] = {0x55, 0x66, 0x77, 0x88, 0x99, 0x55};
unsigned char p[] = {','};


for(int i = 0; i < 6; i++)
{
for(int j =0; j <6 ; j++)
{
p[i] = a[i] ^= b[j];
}
}

cout<< "\n\nExercise 7#\n\n";
for (int i=0;i<6;i++)
{
cout << hex << setfill('0') << setw(2) << int(p[i])<<','<<"\t";
}

I'm really stuck with the varying lengths aspect of the problem.
What do you plan on comparing b[4] and b[5] to? You can't compare it to the memory outside of a[]. You may not have access to it, causing a crash in your program or you may just get garbage data.

You should only compare what you have. Here is a potential solution:
1
2
3
4
5
6
7
8
9
const int size = 6;
unsigned char a[size] = {0x11, 0x22, 0x33, 0x44, 0, 0};
unsigned char b[size] = {0x55, 0x66, 0x77, 0x88, 0x99, 0x55};
unsigned char p[size] = {0};

for(int i = 0; i < 6; i++)
{
    p[i] = a[i] ^ b[i];
}


Last edited on
Topic archived. No new replies allowed.