我如何转换结构系统。字节字节[]到c#中的System.IO.Stream对象?
当前回答
如果在这里的其他MemoryStream示例中出现错误,则需要将Position设置为0。
public static Stream ToStream(this bytes[] bytes)
{
return new MemoryStream(bytes)
{
Position = 0
};
}
其他回答
将字节数组转换为流最简单的方法是使用MemoryStream类:
Stream stream = new MemoryStream(byteArray);
查看MemoryStream类。
你在找记忆流。写方法。
例如,下面的代码将把一个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);
}
}
推荐文章
- Selenium c# WebDriver:等待元素出现
- 如何修剪空白的数组值在php
- 我如何添加双引号的字符串,是在一个变量?
- 如何创建数组。包含不区分大小写的字符串数组?
- 检查字符串是否包含字符串列表中的元素
- 最好的方法在asp.net强制https为整个网站?
- 将字符串转换为System.IO.Stream
- 比较JUnit断言中的数组,简洁的内置方式?
- 数组到哈希Ruby
- 如何从枚举中选择一个随机值?
- 驻留在App_Code中的类不可访问
- 在链式LINQ扩展方法调用中等价于'let'关键字的代码
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API