C# – Convert uint[] to byte[]

arraysbytec++uint32

This might be a simple one, but I can't seem to find an easy way to do it. I need to save an array of 84 uint's into an SQL database's BINARY field. So I'm using the following lines in my C# ASP.NET project:

//This is what I have
uint[] uintArray;

//I need to convert from uint[] to byte[]
byte[] byteArray = ???

cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;

So how do you convert from uint[] to byte[]?

Best Answer

How about:

byte[] byteArray = uintArray.SelectMany(BitConverter.GetBytes).ToArray();

This'll do what you want, in little-endian format...