我如何转换结构系统。字节字节[]到c#中的System.IO.Stream对象?
当前回答
如果在这里的其他MemoryStream示例中出现错误,则需要将Position设置为0。
public static Stream ToStream(this bytes[] bytes)
{
return new MemoryStream(bytes)
{
Position = 0
};
}
其他回答
你在找记忆流。写方法。
例如,下面的代码将把一个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);
}
}
将字节数组转换为流最简单的方法是使用MemoryStream类:
Stream stream = new MemoryStream(byteArray);
查看MemoryStream类。
推荐文章
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- JavaScript在数组中
- c#对象列表,我如何得到一个属性的和
- Ruby数组到字符串的转换
- 我如何使用IValidatableObject?
- 如何指定最小值,但没有使用范围数据注释属性的最大小数?
- 如何分割(块)一个Ruby数组成X元素的部分?
- 如何在PowerShell中获得本地主机名?
- c# vs Java Enum(适合c#新手)
- c#消毒文件名
- 为什么在Java和。net中不能修改字符串?