using System; using System.Collections; using System.Text; namespace TestConsole { class Program { static void Main(string[] args) { var bits = new BitArray(4); bits.SetAll(true); Console.WriteLine(bits.ToInt()); Console.WriteLine(bits.ToStr()); Console.ReadKey(); } } public static class BitArryExtention { /// <summary> /// 将BitArray转为整数 /// </summary> /// <param name="bitArray"></param> /// <returns></returns> public static int ToInt(this BitArray bitArray) { int j = 0; for (int i = 0; i < bitArray.Count; i++) { j += (bitArray[i] ? 1 : 0) << i; } return j; } public static string ToStr(this BitArray bitArray) { StringBuilder j = new StringBuilder(); foreach (bool bit in bitArray) { j.Append(bit ? 1 : 0); } return j.ToString(); } } }