C# convert from uint[] to byte[]

ahmd0 picture ahmd0 · Nov 7, 2011 · Viewed 9.2k times · Source

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[]?

Answer

Matt picture Matt · Nov 7, 2011

How about:

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

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