如何在 C# 中将字节数组转换为对象流?
Stream 是所有流的抽象基类,可提供字节序列的通用视图。流对象涉及三个基本操作,即读取、写入和查找。可以重置流,从而提高性能。
可以使用 MemoryStream 类将字节数组转换为内存流。
MemoryStream stream = new MemoryStream(byteArray);
示例
我们考虑一个包含 5 个值 1、2、3、4、5 的字节数组。
using System;
using System.IO;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
byte[] byteArray = new byte[5] {1, 2, 3, 4, 5 };
using (MemoryStream stream = new MemoryStream(byteArray)) {
using (BinaryReader reader = new BinaryReader(stream)) {
for (int i = 0; i < byteArray.Length; i++) {
byte result = reader.ReadByte();
Console.WriteLine(result);
}
}
}
Console.ReadLine();
}
}
}输出
以上代码的输出为
1 2 3 4 5
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP