Convert bool[] to byte[]

前端 未结 7 1766
野趣味
野趣味 2020-12-09 06:32

I have a List which I want to convert to a byte[]. How do i do this? list.toArray() creates a bool[].

7条回答
  •  悲&欢浪女
    2020-12-09 06:59

    Another LINQ approach, less effective than @hfcs101's but would easily work for other value types as well:

    var a = new [] { true, false, true, true, false, true };
    byte[] b = a.Select(BitConverter.GetBytes).SelectMany(x => x).ToArray();
    

提交回复
热议问题