比较char和unsigned char的每一位 [英] Compare every bit of char versus unsigned char
问题描述
我需要比较以下值:
char buf [3];
/ * buf在这里使用COMport填充。 .. * /
if(buf [0] == 0x85){
/ *做点什么* /
}
但是因为0x85大于char'的最大值,所以这不会真的是b
。那么如何将char的所有位与另一个字节进行比较,例如
0x85在这个例子中???
我正在考虑转换char对于unsigned char,在
if语句中。但我不确定这是否能完美运作...
问候,弗兰克
< blockquote>
" jamx" <德********* @ gmail.com>在消息中写道
news:11 ********************* @ i39g2000cwa.googlegro ups.com ...
我需要比较以下值:
char buf [3];
/ * buf在这里使用COMport填充... * /
if(buf [0] == 0x85){
/ *做点什么* /
}
但是因为0x85大于char'的最大值这不会真的有效。那么如何将char的所有位与另一个字节进行比较,例如在这个例子中是
0x85 ???
我正在考虑将char转换为unsigned char,在
if-statement。但我不确定这是否能完美运行......
*(unsigned char *)buf检查位模式。解释为base-2。
(unsigned char)当buf [0]为负时,buf [0]产生256 - buf [0],因为
我们是假设8位。结果是一样的。
除非你有一台机器,其算术不是2'的补码但是
本机字符类型是签名的尽管如此。如果你有这样一台机器,那就跑得很快。不要尝试编程。 (无论如何,
对于这样的机器来说这个问题不可能得到解决,对于从输入填充阵列所涉及的转换,它将取决于
来源)
-
RSH
将buf [..]定义为''unsigned char''而不是''char'',然后它应该
工作
欢呼
- Ranjeet
" Robin Haigh"写道:
jamx <德********* @ gmail.com>写道:我需要比较以下值:
char buf [3];
/ * buf在这里使用COMport填充.. 。* /
if(buf [0] == 0x85){
/ *做点什么* /
}
但是因为0x85更大然后char'的最大价值,这不会真的有效。那么如何将char的所有位与另一个字节进行比较,
如本例中的0x85 ???
我在考虑将char转换为unsigned char,在
if语句中。但我不确定这是否能完美运行......
*(unsigned char *)buf检查位模式。解释为
base-2。
(unsigned char)当buf [0]为负时,buf [0]产生256 - buf [0],因为
我们是假设8位。结果是一样的。
除非你有一台机器,算术不是2'的补充,但
本机字符类型是签名的,尽管如此。如果你有这样的机器,跑得很快。不要尝试编程。 (在任何情况下,对于这样一台机器来说这个问题不可能得到解决,它会依赖于从输入填充数组所涉及的转换。
来源)
-
RSH
我看不出有什么理由
if(!(buf [0] ^ 0x85)){/ *任意代码* /}
在任何情况下都不应该工作......
也许我在这里错过了一些小细节......可能是。
问候
John
-
你可以拥有它:
快速。
准确。
便宜。
选择两个。
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
"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
define buf[..] as ''unsigned char'' instead of ''char'', then it should
work
cheers
- Ranjeet
"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.
这篇关于比较char和unsigned char的每一位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!