Reputation: 51
Okay, I'm trying to convert a byte[] to a short[], or Int16[].
List<Int16[]> lol = new List<Int16[]>();
byte[] b = System.Text.Encoding.Default.GetBytes("lolololololololololololoolol");
lol.Add(Convert.ToInt16(b));
MessageBox.Show(Encoding.Default.GetString(Encoding.Default.GetBytes(lol[0])));
That is something that I tried, but obviously, it doesn't work. So how would I do this?
Upvotes: 0
Views: 1892
Reputation:
It looks to me like you want to convert an entire array in one line. It could be done like this:
List<Int16[]> lol = new List<Int16[]>();
byte[] b = System.Text.Encoding.Default.GetBytes("lolololololololololololoolol");
lol.Add(Array.ConvertAll(b, x => Convert.ToInt16(x)));
Upvotes: 1
Reputation: 1
byte[] by = new byte[5];
short[] sh = new short[5];
by[0] = 0x1;
by[1] = 0x2;
by[2] = 0x3;
by[3] = 0x4;
by[4] = 0x5;
for (int x = 0; x < sh.GetLength(0); x++)
{
sh[x] = by[x];
MessageBox.Show(by[x].ToString());
That worked for me. Not sure if I am misunderstanding or not.
Upvotes: 0
Reputation: 283624
You probably want BitConverter.ToInt16()
, which you'd need to call for each pair of bytes.
Or, use Buffer.BlockCopy
to do it all at once (using the machine's native byte order).
Upvotes: 0
Reputation: 6578
You have to go through the byte array, and convert each element.
List<Int16[]> lol=new List<Int16[]>();
byte [] b=System.Text.Encoding.Default.GetBytes("lolololololololololololoolol");
Int16 [] a=new Int16 [b.Length];
for (Int32 i=0;i<a.Length;++i) {
a[i]=Convert.ToInt16(b[i]);
}
lol.Add(a);
Upvotes: 0