C# 中的无符号字符指针?
在将一些代码从 C++ 翻译成 C# 的过程中,我发现了这个,
unsigned char *p = m_pRecvBuffer + 5;
unsigned int noncelen1 = *p * 256 + p[1];
如何将其翻译成 C#? m_pRecvBuffer
是一个字符数组,但我将其存储为字节数组。
In the middle of translating some code from C++ to C#, I found this,
unsigned char *p = m_pRecvBuffer + 5;
unsigned int noncelen1 = *p * 256 + p[1];
How do I translate this into C#? m_pRecvBuffer
is a char-array, however I store it as a byte-array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,我想知道是否需要对这段代码进行一些重构。是的,您可以在 C# 中使用指针。然而,基于该片段可能有更好的选择。看起来您正在尝试读取传入流的部分内容。也许 C# 的流库更适合这部分你的代码?
Hmm I wonder if some refactoring would be in order for this piece of code. Yes you can use pointers in C#. However, based on that snippet there may be better options. It looks like you're trying to read parts of an incoming stream. Maybe C#'s stream library would work better for this piece of your code?
您分析代码实际执行的操作并翻译行为,而不是代码。虽然您可以在 C# 中使用
不安全
方法和指针算术,但这可能会产生比它解决的问题更多的问题。You analyse what the code actually does and translate behaviour, not code. While you could use
unsafe
methods and pointer arithmetic in c#, this will probably create more problems than it will solve.类似于
但在这种情况下,我认为您实际上不需要使用数组副本。 我想,只要使用
就足够了。
Something akin to
But in that case I don't think you actually need to use an array copy. Just using
should be enough, I guess.
假设 RecvBuffer 被声明为 byte[],你会这样做:
Assuming RecvBuffer is declared as byte[], you would do something like this: