using System.Runtime.InteropServices;
public static void Main()
Console.WriteLine("Hello World");
decimal d = decimal.Parse("1.015351E+07", System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint);
short[] a= new short[10];
for(int i=0;i<a.Length;i++)
a[i] = (short)rand.Next(1,65535);
var bytes = a.ToByteArray();
Console.WriteLine(BitConverter.ToString(bytes));
var shortA = bytes.ToArray<ushort>();
Console.WriteLine(string.Join(",",shortA));
Console.WriteLine(string.Join(",",a));
public static class ByteConvertExtension
public static byte[] ToByteArray<T>(this T[] senders) where T:struct
byte[] buffer = new byte[senders.Length * Marshal.SizeOf<T>()];
Buffer.BlockCopy(senders, 0, buffer, 0, buffer.Length);
public static T[] ToArray<T>(this byte[] senders)
T[] buffer = new T[senders.Length / Marshal.SizeOf<T>()];
Buffer.BlockCopy(senders, 0, buffer, 0, buffer.Length * Marshal.SizeOf<T>());
public static T[] ToArray<T>(this byte[] senders,int offset)
T[] buffer = new T[(senders.Length - offset) / Marshal.SizeOf<T>()];
Buffer.BlockCopy(senders, offset, buffer, 0, buffer.Length * Marshal.SizeOf<T>());
public static T[] ToArray<T>(this byte[] senders, int offset,int count)
T[] buffer = new T[count / Marshal.SizeOf<T>()];
Buffer.BlockCopy(senders, offset, buffer, 0, buffer.Length * Marshal.SizeOf<T>());