07-10-2006 02:16 PM
07-10-2006 02:26 PM - edited 07-10-2006 02:26 PM
It is not so much a bug in CVI as it is an ambiguous part of the c language. In this case the comparison is not done at the level of char but is promoted to something more native to the processor architecture. 0xA0 and char A0 can be promoted differently due to sign extension. Sign extension converts A0 to 0xFFA0 For this comparison to work the way you expect and to be more portable try this
char c = 0xA0;
if ((c&0x0FF)== 0x0A0)
printf("sucess!");
else
printf("failure");
Message Edited by mvr on 07-10-2006 02:27 PM
07-10-2006 02:43 PM
07-10-2006 02:47 PM
07-10-2006 02:50 PM - edited 07-10-2006 02:50 PM
Original reply:
Another thing you can do is use a type cast on the value you are comparing against. I prefer this approach because then you are comparing apples to apples and sign extension is a non-issue because both values will be treated the same in any sort of conversion.
char c = 0xA0;
if (c == (char) 0xA0)
printf("sucess!");
else
printf("failure");
EDIT: As Roberto suggests, declaring c as unsigned char will prevent the sign extension when the char is promoted to int for the comparison. I'm actually not sure my recommendation above will work because I am unsure if the compiler will also sign-extend the literal value (I suppose I could test it out).
Anyway, the approach below will definitely work (and it has the advantage of not performing an unnecessary bitwise and operation):
unsigned char c = 0xA0;
if (c == (unsigned char) 0xA0)
printf("sucess!");
else
printf("failure");
Message Edited by MJF on 07-10-2006 12:57 PM
07-10-2006 07:33 PM
07-11-2006 09:48 AM
Or even better, compare chars to chars. Use the hexadecimal character notation below. This will definitely be the most portable. I'm not sure why I didn't think of this when I posted yesterday.
char c = '\xA0';
if (c == '\xA0')
printf("sucess!");
else
printf("failure");