C#读取DWORD块中的文件



我正在尝试将二进制文件读取到字节数组中。

我需要读取DWORD块(或4个字节(中的文件,并将每个块存储到字节数组的单个元素中。这就是我迄今为止所取得的成就。

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var block = new byte[4];
while (true)
{
byte[] temp = new byte[4];
fs.Read(temp, 0, 4);
uint read = (byte)BitConverter.ToUInt32(temp, 0);
block[0] = read???
}
}

然而,将uint read转换为block[0]处的元素是不起作用的。我似乎找不到一种不出错的方法。

感谢您的意见。

// read all bytes from file
var bytes = File.ReadAllBytes("data.dat");
// create an array of dwords by using 4 bytes in the file
var dwords = Enumerable.Range(0, bytes.Length / 4)
.Select(index => BitConverter.ToUInt32(bytes, index * 4))
.ToArray();
// down-casting to bytes
var dwordsAsBytes = dwords.Select(dw => (byte)dw).ToArray();

最新更新