Convert byte [] to short [] half-length array
I have byte[200]
one that reads from a file representing short[100]
little-endian format. This is how I read it:
using (FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Read))
{
//fs.Seek(...)
byte[] record = new byte[200];
fs.Read(record, 0, record.Length);
short[] target = new short[100];
// magic operation that fills target array
}
I don't know what to put into the "magic operation". I read about BitConverter
, but he has no operation BitConverter.ToShort
. Anyway, the BitConverter seems to be converted in a loop, whereas I would appreciate a way to "copy" the entire array at once, if possible.
source to share
I think what you are looking for Buffer.BlockCopy
.
Buffer.BlockCopy(record, 0, target, 0, record.Length);
I believe this will keep the architecture you are in legally, so it might not be appropriate in some environments. You might want to distract it by calling a method that can check (one time) if it does what you want (for example, by converting {0, 1} and looking at the result {1} or {256}), and then either uses it Buffer.BlockCopy
or does it "manually" in a loop if necessary.
source to share