Java的&QUOT的等效; ByteBuffer.putType()"在C#中 [英] Equivalent of Java's "ByteBuffer.putType()" in C#

查看:232
本文介绍了Java的&QUOT的等效; ByteBuffer.putType()"在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图格式化C#中的字节数组,通过移植从Java代码。在Java中,方法buf.putInt(值);的使用,buf.putShort,buf.putDouble,(等等)。不过,我不知道如何端口这为C#。我曾尝试将MemoryStream类,但没有方法将在字节数组的结尾特定类型

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.

问:什么是Java的等效的ByteBuffer .putType在C#(值)?
谢谢!

Question: What is the equivalent of Java's "ByteBuffer.putType(value)" in C#? Thanks!

推荐答案

您可以使用的的BinaryWriter 和你的MemoryStream:

You can use a BinaryWriter and your MemoryStream:

MemoryStream stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
    writer.Write(myByte);
    writer.Write(myInt32);
    writer.Write("Hello");
}

byte[] bytes = stream.ToArray();

这篇关于Java的&QUOT的等效; ByteBuffer.putType()"在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆