#define 宏的 C 到 C# 转换
您将如何将以下 C #define 转换为 c#。
#define get16bits(d) (*((const uint16_t *) (d)))
#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+(uint32_t)(((const uint8_t *)(d))[0]) )
#endif
我知道您可能会用 UInt32 替换 uint32_t ,将其他类型更改为 C# 等效类型,但是如何继续将上述方法设为静态方法。这是最好的解决办法吗?
鲍勃.
How would you go about converting the following C #define into c#.
#define get16bits(d) (*((const uint16_t *) (d)))
#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+(uint32_t)(((const uint8_t *)(d))[0]) )
#endif
I know you probably replace the uint32_t which with UInt32 change the other types to c# equivalent, but how proceed from making the above a static method. Would that be the best way of going about it.
Bob.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么您要检查
get16bits
是否在定义后立即定义,因为唯一的方法是预处理器错误,它会停止您的编译。现在,这就是如何将那个可怕的宏转换为 C# 的方法:
事实上,这就是如何将该宏转换为 C# 的方法:
您不需要所有这些转换魔法只是为了获得数字的低 16 位。这里有更多 C 定义来向您展示我正在谈论的内容:
您真的在生产代码中使用过该宏吗?
I do not know why you are checking to see if
get16bits
is defined immediately after you define it, as the only way it would not be is a preprocessor error which would stop your compile.Now, that said, here's how you translate that godawful macro to C#:
In fact, here's how you translate that macro to C:
You don't need all this casting wizardry just to get the lower 16 bits of a number. Here's more C defines to show you what I'm talking about:
Have you really used that macro in production code?
在 C# 中获取整数的低 16 位非常容易:
如果您想要一个静态方法来完成此操作,也很简单:
Getting the lower 16 bits of an integer is quite easy in C#:
If you want a static method for doing it, it's just as simple: