Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compare every bit of char versus unsigned char

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
ranjmis
Guest
Posts: n/a
 
      03-07-2006
define buf[..] as 'unsigned char' instead of 'char', then it should
work

cheers
- Ranjeet

 
Reply With Quote
 
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.


 
Reply With Quote
 
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.

 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
jamx
Guest
Posts: n/a
 
      03-07-2006
Thanks pete !!! this way it works perfect

 
Reply With Quote
 
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
header.
 
Reply With Quote
 
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
in your environment.

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
how to place two unsigned 16 bit values into a 32 bit unsigned value pasamdivya@gmail.com C++ 3 11-19-2008 02:09 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments