Load a (0/1) string into a bit array

↘锁芯ラ 提交于 2019-12-01 17:26:56

You can do it with LINQ:

var res = new BitArray(str.Select(c => c == '1').ToArray());

You can use LINQ on this case like;

var yourbitarray = new BitArray(yourstring.Select(s => s == '1').ToArray());

How about something like this:

string bits = "101010101010";
byte[] bytes = bits.ToCharArray().Select(c => (byte)c == '0' ? 0 : 1).ToArray();

Might work...

or

byte[] bytes = bits.Select(c => (byte)c == '0' ? 0 : 1).ToArray();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!