3

Is there a more efficient way to convert byte array to int16 array ?? or is there a way to use Buffer.BlockCopy to copy evry two byte to int16 array ???

public static int[] BYTarrToINT16arr(string fileName)
{
try
{
int bYte = 2;
byte[] buf = File.ReadAllBytes(fileName); 
int bufPos = 0;
int[] data = new int[buf.Length/2];
byte[] bt = new byte[bYte];
for (int i = 0; i < buf.Length/2; i++)
{
Array.Copy(buf, bufPos, bt, 0, bYte);
bufPos += bYte;
Array.Reverse(bt);
data[i] = BitConverter.ToInt16(bt, 0);
}
return data;
}
catch
{
return null;
}
}   
4
  • Please don't add things like "C#" to the ends of your titles. That's what the tags are for. Commented Mar 26, 2012 at 2:28
  • Also, I strongly suggest you get rid of that try/catch block. If there's an exception, then you want to know about it. Commented Mar 26, 2012 at 2:28
  • P.S. return type is int[] not Int16[] Apologies for for confusion Commented Mar 26, 2012 at 5:32
  • Why aren't you using Buffer.BlockCopy like you mention? Commented May 28, 2012 at 7:12

5 Answers 5

4

Use a FileStream and a BinaryReader. Something like this:

var int16List = List<Int16>();
using (var stream = new FileStream(filename, FileMode.Open))
using (var reader = new BinaryReader(stream))
{
    try
    {
        while (true)
            int16List.Add(reader.ReadInt16());
    }
    catch (EndOfStreamException ex)
    {
        // We've read the whole file
    }
}
return int16List.ToArray();

You can also read the whole file into a byte[], and then use a MemoryStream instead of the FileStream if you want.

If you do this then you'll also be able to size the List approrpriately up front and make it a bit more efficient.

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

2 Comments

If you want an int[] (rather than Int16[]) then just use a List<int> to build the array in the first place.
I want 2-2 byte converted to int[]...if i use List<int>..it will grab 4 bytes and convert to int32...right ??
1

Apart from having an off-by-one possibility in case the number of bytes is odd (you'll miss the last byte) your code is OK. You can optimize it by dropping the bt array altogether, swapping i*2 and i*2+1 bytes before calling BitConverter.ToInt16, and passing i*2 as the starting index to the BitConverter.ToInt16 method.

Comments

1

This works if you don't mind using interopservices. I assume it is faster than the other techniques.

using System.Runtime.InteropServices;   

public Int16[] Copy_Byte_Buffer_To_Int16_Buffer(byte[] buffer)

{
    Int16[] result = new Int16[1];
    int size = buffer.Length;
    if ((size % 2) != 0)
    {
        /* Error here */
        return result;
    }
    else
    {
        result = new Int16[size/2];
        IntPtr ptr_src = Marshal.AllocHGlobal (size);
        Marshal.Copy (buffer, 0, ptr_src, size);
        Marshal.Copy (ptr_src, result, 0, result.Length);
        Marshal.FreeHGlobal (ptr_src);
        return result;
    }
}

Comments

0
var bytes = File.ReadAllBytes(path);

var ints = bytes.TakeWhile((b, i) => i % 2 == 0).Select((b, i) => BitConverter.ToInt16(bytes, i));

if (bytes.Length % 2 == 1)
{
    ints = ints.Union(new[] {BitConverter.ToInt16(new byte[] {bytes[bytes.Length - 1], 0}, 0)});
}

return ints.ToArray();

Comments

0

try...

int index = 0;            
var my16s = bytes.GroupBy(x => (index++) / 2)
           .Select(x => BitConverter.ToInt16(x.Reverse().ToArray(),0)).ToList();

Comments

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.