定义2个字节的最佳方法,以便在C中进行数组初始化/比较时使用?(Best way to define 2 bytes for easy use with array initialization /

编程入门 行业动态 更新时间:2024-10-26 13:36:11
定义2个字节的最佳方法,以便在C中进行数组初始化/比较时使用?(Best way to define 2 bytes for easy use with array initialization / comparing in C?)

再说一次,我对C来说相当新,所以原谅这个问题的简单/愚蠢。 无论如何,它在这里。 在C中定义一个2字节宏(即#define MSGID 0xABCD)的最佳方法是什么?它很容易放入字节数组,但也比较if语句的内容?

为了澄清这一点,请再次使用0xABCD示例。 说我想做一个:

unsigned char test_msg[] = { 0x00, 0x01, 0x02, MSGID, 0x03, 0x04, 0x05 };

根据我在上面的定义,它将无法工作,因为它被定义为一个大的int,gcc将它截断为无符号类型。 当我将它定义为#define MSGID 0xAB,0xCD它似乎工作正常,但我不明白逗号在那里做什么,看起来不是很干净。 我找到的另一个选项是工作,但也不干净,将其分解为#define MSGIDCLA​​SS 0xAB然后#define MSGID 0xCD。

我还想比较这些MSGID字节,目前我不得不经常做一个if(data [n] == MSGIDCLA​​SS && data [n + 1] == MSGID)...对于我需要的每条消息解析并回应。 我想知道是否有更简单的方法来做到这一点,就是这样。 如果没有,我会保持原样。 再次感谢您的帮助。 很抱歉没有使用代码示例代码,不认为有一个衬里是必要的。

Again, I am fairly new to C so forgive the simplicity / stupidity of this question. Anyway, here it goes. What is the best way to #define a 2 byte macro (i.e. #define MSGID 0xABCD) in C that is easy to put inside a byte array but also compare the contents of with if statements?

To clarify this, take the 0xABCD example again. Say I want to do an:

unsigned char test_msg[] = { 0x00, 0x01, 0x02, MSGID, 0x03, 0x04, 0x05 };

With how I defined it above, it won't work because it's defined as a large int and gcc truncates it to an unsigned type. When I define it as #define MSGID 0xAB,0xCD it seems to work fine, but I don't understand what the comma does there and that doesn't look very clean. Another option I found that works, but also not clean, is splitting it like #define MSGIDCLASS 0xAB then #define MSGID 0xCD.

I would also like to compare these MSGID bytes and at the moment I'm having to constantly do an if(data[n] == MSGIDCLASS && data[n+1] == MSGID) ... for every message I need to parse and respond to. I was wondering if there was an easier way of doing this, that's all. If not, I'll leave it as is. Thanks again for the help. Sorry for not using the code sample tags, didn't think they were necessary with one liners.

最满意答案

#define只是被他们的内容所取代。 因此,当您使用逗号时,最终代码变为:

unsigned char test_msg[] = { 0x00, 0x01, 0x02, 0xAB, 0xCD, 0x03, 0x04, 0x05 };

这就是它似乎工作的原因。

相反,单独定义MSGIDHI和MSGIDLO组件可以让您轻松使用和比较。 您可以将MSGID定义为:

#define MSGIDLO 0xCD #define MSGIDHI 0xAB #define MSGID (MSGIDLO | (MSGIDHI << 8))

这样您就可以以任何您想要的形式使用它们。

#define's simply get replaced with their contents. So when you use commas the final code becomes:

unsigned char test_msg[] = { 0x00, 0x01, 0x02, 0xAB, 0xCD, 0x03, 0x04, 0x05 };

That's why it seems to work.

Instead, defining MSGIDHI and MSGIDLO components separately allows you to both use and compare easily. And you can define MSGID as:

#define MSGIDLO 0xCD #define MSGIDHI 0xAB #define MSGID (MSGIDLO | (MSGIDHI << 8))

This way you can use them in any form you want.

更多推荐

本文发布于:2023-08-04 19:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   初始化   字节   定义   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!