Is there a way to read binary data from file into an array like in C where I can pass a pointer of any type to the i/o functions? I am thinking of something like BinaryReader::ReadBytes(), but that returns a byte[] which I cannot cast to the desired array pointer type.
Share
If you have a fixed size
structYou can then read it in in one go using this:
This reads in a byte array covering the size of the
structand then marshals the byte array into the structure. You can use it like this:The
structmay include array types as long as the array length is specified, i.e:Edit:
I see you just want to read in a
shortarray – In this case just read in the byte array and useBuffer.BlockCopy()to convert to the array you want:This is quite efficient, equivalent to a
memcpyin C++ under the hood. The only other overhead you have of course is that the original byte array will be allocated and later garbage collected. This approach would work for any other primitive array type as well.