Search code examples
ctype-conversionunsigned-integerunsigned-charmplab

comparison between unsigned int and unsigned char


I have an unsigned char array, such as Data[2]. I needed it to compare with an output of a function returning unsigned int.

i tried to cast the Data[2] into unsigned int and vice versa. It didn't work.

What I am trying to do is:

if (Data[2] == ReadFlash2(40))
{
    //Do Something.
}

ReadFlash2 is a function returning unsigned int, while Data[2] is a unsigned char.

I tried to cast each of them, but it didn't work.

Is there something I am doing wrong? Which one should I cast, and to what should I cast it?

Thanks.

Edit: the code for the Readflash function:

unsigned int ReadFlash2(unsigned int Addr) // use as Read Function 
{
pMem = (unsigned int*)MEM_STR_ADR; 
pMem += Addr; 
Nop(); 
return(*pMem); 
}

Solution

  • It appears that you are accessing volatile memory on the microcontroller, and this could be causing some confusion when debugging. Try storing Data[2] and ReadFlash(40) into variables before the comparison:

    unsigned char data_2 = Data[2];
    unsigned int readflash2_40 = ReadFlash2(40);
    
    if (data_2 == readflash2_40)
    {
        //Do Something.
    }
    

    Now you should be able to inspect the actual values that are being compared by looking at the values in the variables.