16

I'm currently reading a file and wanted to be able to convert the array of bytes obtained from the file into a short array.

How would I go about doing this?

1
  • 21
    Do you want to convert 1 byte to 1 short, or 2 bytes to 1 short? Commented Jul 9, 2009 at 15:27

7 Answers 7

70

Use Buffer.BlockCopy.

Create the short array at half the size of the byte array, and copy the byte data in:

short[] sdata = new short[(int)Math.Ceiling(data.Length / 2)];
Buffer.BlockCopy(data, 0, sdata, 0, data.Length);

It is the fastest method by far.

Sign up to request clarification or add additional context in comments.

2 Comments

The solution i don't deserve, but the one i need right now!
Thanks! Great answer but I had to change the code slightly to: short[] sdata = new short[(int)Math.Ceiling(data.Length / 2.0)];. Otherwise, I would have gotten the following error: "The call is ambiguous between the following methods or properties: 'Math.Ceiling(decimal)' and 'Math.Ceiling(double)' "
16

One possibility is using Enumerable.Select:

byte[] bytes;
var shorts = bytes.Select(b => (short)b).ToArray();

Another is to use Array.ConvertAll:

byte[] bytes;
var shorts = Array.ConvertAll(bytes, b => (short)b);

4 Comments

Your original suggestion (before you added the second one later on) is rather inefficient
Another option would be bytes.Cast<short>().ToArray();
Actually, this results in a InvalidCastException. The short explanation is that this code implicitly causes an boxed byte to be unboxed to a short which is not a valid unboxing operation. For details, see stackoverflow.com/questions/445471/….
When I used var shorts[] = Array.ConvertAll(bytes, b => (short)b); I got each byte converted to a short not each pair of bytes converted.
6

A shorthard is a compound of two bytes. If you are writing all the shorts to the file as true shorts then those conversions are wrong. You must use two bytes to get the true short value, using something like:

short s = (short)(bytes[0] | (bytes[1] << 8))

2 Comments

This doesn't work. You have to do it like this to make it work: short s = (short)((bytes[0] << 8) | bytes[1]);
Assuming little endian, this should be: short s = (short)(bytes[0] | (bytes[1] << 8))
2
short value = BitConverter.ToInt16(bytes, index);

2 Comments

This one uses index as LSB and index+1 as MSB. Is there one that follows BigEndian to use index as the MSB?
Did you even read the question? It is about byte[] to short[] not short.
1

I dont know, but I would have expected another aproach to this question. When converting a sequence of bytes into a sequence of shorts, i would have it done like @Peter did

short s = (short)(bytes[0] | (bytes[1] << 8))

or

short s = (short)((bytes[0] << 8) | bytes[1])

depending on endianess of the bytes in the file.

But the OP didnt mention his usage of the shorts or the definition of the shorts in the file. In his case it would make no sense to convert the byte array to a short array, because it would take twice as much memory, and i doubt if a byte would be needed to be converted to a short when used elsewhere.

Comments

0
 short[] wordArray = Array.ConvertAll(byteArray, (b) => (short)b);

1 Comment

In my opinion, this would convert every byte into a short, which might not be the expected result als two bytes would represent a short ;-)
-2
byte[] bytes;
var shorts = bytes.Select(n => System.Convert.ToInt16(n)).ToArray();

3 Comments

That's extremely inefficient: Calling convert.ToInt16() for every element, storing it in a temporary list, and then copying it to an new array.
yes, it is inefficient. I'm thinking that it's safer, tho, then casting.
Safer than casting? A cast of byte to short always works. It can never throw an exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.