Converting a list of ints to a byte array

后端 未结 4 2046
旧巷少年郎
旧巷少年郎 2021-01-17 12:26

I tried to use the List.ConvertAll method and failed. What I am trying to do is convert a List to byte[]

I copped out and went

4条回答
  •  [愿得一人]
    2021-01-17 13:20

    Since you don't want a byte[][] where each integer maps to an array of four bytes, you cannot call ConvertAll. (ConvertAll cannot perform a one-to-many conversion)

    Instead, you need to call the LINQ SelectMany method to flatten each byte array from GetBytes into a single byte[]:

    integers.SelectMany(BitConverter.GetBytes).ToArray()
    

提交回复
热议问题