473,378 Members | 1,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Compare every bit of char versus unsigned char

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

Mar 7 '06 #1
11 5891

"jamx" <de*********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro 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
Mar 7 '06 #2
define buf[..] as 'unsigned char' instead of 'char', then it should
work

cheers
- Ranjeet

Mar 7 '06 #3
"Robin Haigh" wrote:

"jamx" <de*********@gmail.com> 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.
Mar 7 '06 #4
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.

Mar 7 '06 #5
"jamx" <de*********@gmail.com> 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
Mar 7 '06 #6
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
Mar 7 '06 #7
Thanks pete !!! this way it works perfect ;-)

Mar 7 '06 #8
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.
Mar 7 '06 #9
"jamx" <de*********@gmail.com> 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) ks***@mib.org <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.
Mar 7 '06 #10

jamx 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...


It depends on what your assumptions are about where the
negative character values come from, and whether the
representation is known to be twos complement or not.
The comparisons

if ((unsigned char)buf[0] == 0x85) {

if (((unsigned char*)buf)[0] == 0x85) {

do the same thing for twos complement, but not for
the other representations. Depending on how values
get from the wire on the COM port into the buffer,
you should use one of these or the other.

Mar 8 '06 #11
On 7 Mar 2006 09:45:52 -0800, "jamx" <de*********@gmail.com> 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
You probably want an & in front of cmd_reply.

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.

Remove del for email
Mar 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Meyer | last post by:
Hi, Does anybody have experience with the performance of CHAR in comparison with VARCHAR? I´ve converted the table from varchar (dynamic format) to char (fixed row length) and the total size of...
37
by: Claude Gagnon | last post by:
Hi, How can we compare sign of numbers in C++ ? Bye, Claude
13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
4
by: slot | last post by:
Is it possible to compare "enum" types to basic types like "unsigned short" type at all? Thanks!
28
by: JKop | last post by:
Haven't been able to find such a thing. Can anyone please inform me of a Standard C++ function for comparing two strings without regard to case. Both for working with "char*", and with...
11
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function...
8
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.