Velocity Reviews > Compare every bit of char versus unsigned char

# Compare every bit of char versus unsigned char

jamx
Guest
Posts: n/a

 03-07-2006
I need to compare the following values:

char buf[3];

/* buf is filled using COMport here... */

if (buf[0] == 0x85) {
/* do something */
}

But since 0x85 is bigger then char's maximum value this won't really
work. So how can i compare all bits of a char with a other byte, like
0x85 in this example???

I was thinking about casting the char to unsigned char, in the
if-statement. But im not sure if that will work flawlessly...

Greetings, Frank

Robin Haigh
Guest
Posts: n/a

 03-07-2006

"jamx" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> I need to compare the following values:
>
> char buf[3];
>
> /* buf is filled using COMport here... */
>
> if (buf[0] == 0x85) {
> /* do something */
> }
>
> But since 0x85 is bigger then char's maximum value this won't really
> work. So how can i compare all bits of a char with a other byte, like
> 0x85 in this example???
>
> I was thinking about casting the char to unsigned char, in the
> if-statement. But im not sure if that will work flawlessly...

*(unsigned char *)buf examines the bit pattern. interpreted as base-2.
(unsigned char)buf[0] yields 256 - buf[0] when buf[0] is negative, since
we're assuming 8 bits. The result is the same.

Except if you have a machine where the arithmetic is not 2's-complement but
the native char type is signed in spite of that. If you have such a
machine, run away very fast. Do not attempt to program it. (In any case,
the question can't be answered portably for such a machine, it would depend
on what conversion was involved in filling the array from the input source)

--
RSH

ranjmis
Guest
Posts: n/a

 03-07-2006
define buf[..] as 'unsigned char' instead of 'char', then it should
work

cheers
- Ranjeet

John F
Guest
Posts: n/a

 03-07-2006
"Robin Haigh" wrote:
>
> "jamx" <(E-Mail Removed)> wrote:
>> I need to compare the following values:
>>
>> char buf[3];
>>
>> /* buf is filled using COMport here... */
>>
>> if (buf[0] == 0x85) {
>> /* do something */
>> }
>>
>> But since 0x85 is bigger then char's maximum value this won't
>> really
>> work. So how can i compare all bits of a char with a other byte,
>> like
>> 0x85 in this example???
>>
>> I was thinking about casting the char to unsigned char, in the
>> if-statement. But im not sure if that will work flawlessly...

>
> *(unsigned char *)buf examines the bit pattern. interpreted as
> base-2.
> (unsigned char)buf[0] yields 256 - buf[0] when buf[0] is negative,
> since
> we're assuming 8 bits. The result is the same.
>
> Except if you have a machine where the arithmetic is not
> 2's-complement but
> the native char type is signed in spite of that. If you have such a
> machine, run away very fast. Do not attempt to program it. (In any
> case,
> the question can't be answered portably for such a machine, it would
> depend
> on what conversion was involved in filling the array from the input
> source)
>
> --
> RSH

I see no reason why

if( !( buf[0] ^ 0x85 )) {/*arbitrary code here*/}

shouldn't work in any case...

Maybe I'm missing some tiny detail here ... could be.

regards
John
--
You can have it:

Quick.
Accurate.
Inexpensive.

Pick two.

jamx
Guest
Posts: n/a

 03-07-2006
I just keep getting this warning when i use ( !(buf[0] ^ 0x85)) :

comparison is always true due to limited range of data type

and when i use: if ((*(unsigned char *)cmd_reply[0] == 0x41)) i get
this warning:

cast to pointer from integer of different size

It might work in some cases.. but compiling with these kinda warnings
isn't really my goal. And i also cannot change buf[] to an unsigned
char since my COM object requires char.

Ben Pfaff
Guest
Posts: n/a

 03-07-2006
"jamx" <(E-Mail Removed)> writes:

> I just keep getting this warning when i use ( !(buf[0] ^ 0x85)) :
>
> comparison is always true due to limited range of data type

Presumably buf[0] is a char and you are in a signed character
environment. In that case, the buf[0] that you're thinking of as
having value 0x85 actually has value -123 (in a 2's complement
environment, if I did the math right). As an int, which it will
normally be promoted to, this is bit pattern 0xffffff85 (in a
32-bit, 2's complement environment). 0xffffff85 ^ 0x85 is
0xffffff00, not 0.

I think this analysis is correct, but I always find this a
confusing area of standard C. I hope my errors, if any, will be
corrected quickly.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

pete
Guest
Posts: n/a

 03-07-2006
jamx wrote:
>
> I just keep getting this warning when i use ( !(buf[0] ^ 0x85)) :
>
> comparison is always true due to limited range of data type
>
> and when i use: if ((*(unsigned char *)cmd_reply[0] == 0x41)) i get
> this warning:
>
> cast to pointer from integer of different size
>
> It might work in some cases.. but compiling with these kinda warnings
> isn't really my goal. And i also cannot change buf[] to an unsigned
> char since my COM object requires char.

You didn't do it right.

if (((unsigned char *)cmd_reply)[0] == 0x41)

--
pete

jamx
Guest
Posts: n/a

 03-07-2006
Thanks pete !!! this way it works perfect

Default User
Guest
Posts: n/a

 03-07-2006
ranjmis wrote:

> define buf[..] as 'unsigned char' instead of 'char', then it should
> work

What should work? It's difficult to tell what you are talking about.
See below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded

Keith Thompson
Guest
Posts: n/a

 03-07-2006
"jamx" <(E-Mail Removed)> writes:
> I need to compare the following values:
>
> char buf[3];
>
> /* buf is filled using COMport here... */
>
> if (buf[0] == 0x85) {
> /* do something */
> }
>
> But since 0x85 is bigger then char's maximum value this won't really
> work. So how can i compare all bits of a char with a other byte, like
> 0x85 in this example???
>
> I was thinking about casting the char to unsigned char, in the
> if-statement. But im not sure if that will work flawlessly...

Plain char may be either signed or unsigned. Apparently it's signed

Is there any reason you can't just declare buf as
unsigned char buf[3];
?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.