我如何转换结构系统。字节字节[]到c#中的System.IO.Stream对象?
当前回答
写入任何流(不仅仅是MemoryStream)的一般方法是使用BinaryWriter:
static void Write(Stream s, Byte[] bytes)
{
using (var writer = new BinaryWriter(s))
{
writer.Write(bytes);
}
}
其他回答
查看MemoryStream类。
将字节数组转换为流最简单的方法是使用MemoryStream类:
Stream stream = new MemoryStream(byteArray);
你在找记忆流。写方法。
例如,下面的代码将把一个byte[]数组的内容写入内存流:
byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream();
stream.Write(myByteArray, 0, myByteArray.Length);
或者,你可以基于字节数组创建一个新的,不可调整大小的MemoryStream对象:
byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream(myByteArray);
如果在这里的其他MemoryStream示例中出现错误,则需要将Position设置为0。
public static Stream ToStream(this bytes[] bytes)
{
return new MemoryStream(bytes)
{
Position = 0
};
}
写入任何流(不仅仅是MemoryStream)的一般方法是使用BinaryWriter:
static void Write(Stream s, Byte[] bytes)
{
using (var writer = new BinaryWriter(s))
{
writer.Write(bytes);
}
}
推荐文章
- _ViewStart的位置和方式。CSHTML布局文件链接?
- 新建T()
- 如何将枚举绑定到WPF中的组合框控件?
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- 使用linq转换列表到字典,不用担心重复
- 单元测试:日期时间。现在
- 什么是回调?
- .NET中的KeyDown和KeyPress有什么区别?
- 如何在Java中将int[]转换为Integer[] ?
- 返回匿名类型的结果?
- 你能解释一下流的概念吗?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async