I'm new and I don't want to waste anyone's time, but I searched and found nothing on this. I am to write a simple CRC-16 routine. My compiler (microsoft) will not accept a char[] as an operand when I try to execute the xor.

char msg[5] = {'A','B','C'};
int generator = 0x8003;
msg ^= generator;


error C2296: '^=' : illegal, left operand has type 'char [5]'

I've seen code all over the place that uses char as an operand. What am I doing wrong? Bear with me, I'm just learning.

Recommended Answers

All 2 Replies

you can't do xor operation on an entire array -- only one element at a time. msg[0] ^= generator;

Ancient Dragon is right, but his solution does not answer the problem. If you XOR 1 char (= 1byte) with the generator (=4bytes) you're actually just x-orring every byte with the last byte from the generator (in this case 0x03) and that's not what you want.

What you want is a loop that loop through your array of char and compares it with the right byte from the generator. You can do this (for example) by bitshifting the generator each time you x-orred a byte from your array:

char msg[]= "ABC";
    int generator = 0x8003;
    int my_gen = generator;
    for (int i = strlen(msg) -1 ; i >= 0; i--)   
    {
        unsigned char ch_xor = my_gen & 0x000000FF; // 32 bits OS
        msg[i] ^= ch_xor; 
        my_gen >>=8; // shift a bit
        
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.