开发者

Read an array of 32bit ints from a binary format file?

OK so I have converted a file into a binary format using BinaryWriter. The format is:

number of ints, followed by the ints.

So the code will be something like:

readLineOfNumbers() {
    count = read();
    int[] a = read(count ints);
    return a;
}

Do I use a BinaryReader? The closest thing I can see there is to read everything into a byte[], but then how do I make that an int array? This all has to be done very efficiently as well. I need buffering and开发者_运维问答 so on.


If you use BinaryWriter to create file it makes sense to read it using BinaryReader

Something like:

private static int[] ReadArray(BinaryReader reader)
    {
        int count = reader.ReadInt32();
        int[] data = new int[count];
        for (int i = 0; i < count; i++)
        {
            data[i] = reader.ReadInt32();
        }
        return data;
    }


I don't know of anything within BinaryReader which will read an array of integers, I'm afraid. If you read into a byte array you could then use Buffer.BlockCopy to copy those bytes into an int[], which is probably the fastest form of conversion - although it relies on the endianness of your processor being appropriate for your data.

Have you tried just looping round, calling BinaryReader.ReadInt32() as many times as you need to, and letting the file system do the buffering? You could always add a BufferedStream with a large buffer into the mix if you thought that would help.


int[] original = { 1, 2, 3, 4 }, copy;
byte[] bytes;
using (var ms = new MemoryStream())
{
    using (var writer = new BinaryWriter(ms))
    {
        writer.Write(original.Length);
        for (int i = 0; i < original.Length; i++)
            writer.Write(original[i]);
    }
    bytes = ms.ToArray();
}
using (var ms = new MemoryStream(bytes))
using (var reader = new BinaryReader(ms))
{
    int len = reader.ReadInt32();
    copy = new int[len];
    for (int i = 0; i < len; i++)
    {
        copy[i] = reader.ReadInt32();
    }
}

Although personally I'd just read from the stream w/o BinaryReader.

Actually, strictly speaking, if it was me I would use my own serializer, and just:

[ProtoContract]
public class Foo {
    [ProtoMember(1, Options = MemberSerializationOptions.Packed)]
    public int[] Bar {get;set;}
}

since this will have known endianness, handle buffering, and will use variable-length encoding to help reduce bloat if most of the numbers aren't enormous.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜