I need to convert a Double[]
to Short[]
to Byte[]
I'm converting to short first, since Double
has to many bits so if I convert it directly to Bytes
I will get blank bytes.
Intially I have a Double[]
called wavPuroDer
filled with information
short[] wavShortDer = ConvertToShort(wavPuroDer);
byte[] wavByteDer = ConvertToByte(wavShortDer);
public static short[] ConvertToShort(double[] doble)
{
short[] shorty = new short[100000000];
for (int i = 0; i < doble.Length; i++)
{
shorty[i]=Convert.ToInt16 (doble[i]);
}
return shorty;
}
public static byte[] ConverToByte(short[] shorty)
{
byte[] bytey = new byte[1000000000];
int j = 0;
for (int i = 0; i < shorty.Length; i++)
{
byte[] byteArray = BitConverter.GetBytes(shorty[i]);
bytey[j] = byteArray[0];
bytey[j+1] = byteArray[1];
j = j + 2;
}
return bytey;
}
my problem is, that im getting a System.IndexOutOfRangeException in the line
bytey[j] = byteArray[0];
and I cant make this array any larger...
Can someone help please? Thanks!
(sorry for my bad English, its no my main language ) `
You could use linq to simplify all this:
byte[] wavByteDer = wavPuroDer.Select(x => Convert.ToInt16(x))
.SelectMany(x => BitConverter.GetBytes(x))
.ToArray();
(Note: just make sure you have using System.Linq;
)