Greetings Overflowers,
I love the flexibility of memory mapped files in that you can read/write any value type.
Is there a way to do the same with byte arrays without having to copy them into for e.g. a memory map buffers ?
Regards
Greetings Overflowers,
I love the flexibility of memory mapped files in that you can read/write any value type.
Is there a way to do the same with byte arrays without having to copy them into for e.g. a memory map buffers ?
Regards
(Using unsafe code) The following sample shows how to fill a 16 byte array with two long values, which is something BitConverter still can't do without an additional copy operation:
Or you could make your own StoreBytes() method:
Reading values from a byte array is no problem with BitConverter since you can specify the offset in .ToInt64.
Alternative : use Buffer.BlockCopy, which can convert between array types.
You can use the
BitConverter
class to convert between base data types and byte arrays.You can read values directly from the array:
To write data you convert it to a byte array, and copy it into the data:
You are searching the
MemoryStream
class which can be initialised (without copying!) from a fixed-size byte array.You can bind a
MemoryStream
to a given byte array, set it's propertyPosition
to go to a specific position within the array, and then use aBinaryReader
orBinaryWriter
to read / write values of different types from/to it.