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

痞子三分冷 提交于 2019-12-01 16:53:35

问题


What is the smartest way to load a string like "10101011101010" directly into a new bit array? (not a byte array)

(The bits should remain in the same order as in the list.)


回答1:


You can do it with LINQ:

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



回答2:


You can use LINQ on this case like;

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



回答3:


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();


来源:https://stackoverflow.com/questions/13951062/load-a-0-1-string-into-a-bit-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!