Converting a list of ints to a byte array

后端 未结 4 2059
旧巷少年郎
旧巷少年郎 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:26

    How about

    var bytes = integers.Select(i => BitConverter.GetBytes(i)).ToArray();
    

    totally untested BTW, but seems reasonable.

    This should actually give you an Array of arrays of bytes...which may or may not be what you need. If you want to collapse it into a single array, you can use SelectMany

    var bytes = integers.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
    

提交回复
热议问题