相当于 Java 的“ByteBuffer.putType()”在 C# 中
我正在尝试通过从 Java 移植代码来格式化 C# 中的字节数组。在 Java 中,使用方法“buf.putInt(value);”、buf.putShort、buf.putDouble(等等)。但我不知道如何将其移植到 C#。我尝试过 MemoryStream 类,但没有方法将特定类型放在字节数组的末尾。
问题:C# 中 Java 的“ByteBuffer.putType(value)”相当于什么? 谢谢!
I am trying to format a byte array in C#, by porting a code from Java. In Java, the methods "buf.putInt(value);", buf.putShort, buf.putDouble, (and so forth) are used. However I don't know how to port this to C#. I have tried the MemoryStream class, but there is no method to put a specific type at the end of the byte array.
Question: What is the equivalent of Java's "ByteBuffer.putType(value)" in C#?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 BinaryWriter 和 MemoryStream:
You can use a BinaryWriter and your MemoryStream:
尝试
BinaryWriter
班级:Try the
BinaryWriter
class:您将需要使用
BitConverter
类。主要区别在于这些方法返回字节数组而不是更改现有数组。(这是对提到的具体方法的替换;对于整个ByteBuffer类的替换,请参见其他回复。)
You'll be wanting to use the
BitConverter
class. The main difference is that these methods return an array of bytes instead of altering an existing array.(This is a replacement for the specific methods mentioned; for a replacement of the entire ByteBuffer class, see the other replies.)