C# convert from uint[] to byte[]

后端 未结 5 2039
[愿得一人]
[愿得一人] 2021-01-13 20:35

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 fol

5条回答
  •  灰色年华
    2021-01-13 21:05

    You can use System.Buffer.BlockCopy to do this:

    byte[] byteArray = new byte[uintArray.Length * 4];
    Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4];
    

    http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

    This will be much more efficient than using a for loop or some similar construct. It directly copies the bytes from the first array to the second.

    To convert back just do the same thing in reverse.

提交回复
热议问题