????0x00 ????
??????????????Щ????????????????????????????????????byte??8bit?????λ??????BinaryReader??????????????????????bool????????byte????????????C#??????????????Щ??????????????????????????????????????????????????????????????????????7?????6?????????ASCII????????????μ?д????????????????????????????????????????????????????????
????0x01 ?????????????
?????????????????byte b = 35????????????????е??4bit???4bit????????????????????????????????????????????????????????????????????????????£??????????????????
????1?????b???????????????00100011
????2???????????4bit?????????????????
????Convert.ToInt32("0010");
?????????????????????????????
?????????????а?byte????????????????к?????????
????1??????Convert.ToString(b??2)??????8λ???λ??0????
????2????????byte?????1??2??4??8 … 128???????????????????λ??
????3????????byte??32????????????byte?????????128????????
???????е??????????????????????????????2??3????????????????????????3???????о??????????£?
????public static char[] ByteToBinString(byte b)
????{
????var result = new char[8];
????for (int i = 0; i < 8; i++)
????{
????var temp = b & 128;
????result[i] = temp == 0 ? '0' : '1';
????b = (byte)(b << 1);
????}
????return result;
????}
??????????byte[]?????????????????????
????Public string BitReader(byte[] data)
????{
????BinString = new StringBuilder(data.Length * 8);
????for (int i = 0; i < data.Length;
????{
????BinString.Append(ByteToBinString(data[i]));
????}
????return BinString.ToString();
????}
????????????????byte[]????????????????????????????????????????????bitλ?ú?bit??????ж???????????????????????bool??Int16??Int32???????????·??????д???BitReader????????StringBuilder?洢?????????????????Read?????????????????ж???????????????????????????????????????????Position????????????????ЩRead??????????????Position??????????????????ReadInt16????????BitReader???Position???λ??????16bit??????Int16???????Position??????16bit?????????????????????????????????λ?????Position?????????????Position????Position?????BitReader??????????£?
1 public class BitReader
2 {
3     public readonly StringBuilder BinString;
4     public int Position { get; set; }
5
6     public BitReader(byte[] data)
7     {
8         BinString = new StringBuilder(data.Length * 8);
9         for (int i = 0; i < data.Length; i++)
10         {
11             BinString.Append(ByteToBinString(data[i]));
12         }
13         Position = 0;
14     }
15
16     public byte ReadByte(int offset)
17     {
18         var bin = BinString.ToString(offset?? 8);
19         return Convert.ToByte(bin?? 2);
20     }
21
22     public byte ReadByte()
23     {
24         var result = ReadByte(Position);
25         Position += 8;
26         return result;
27     }
28
29     public int ReadInt(int offset?? int bitLength)
30     {
31         var bin = BinString.ToString(offset?? bitLength);
32         return Convert.ToInt32(bin?? 2);
33     }
34
35     public int ReadInt(int bitLength)
36     {
37         var result = ReadInt(Position?? bitLength);
38         Position += bitLength;
39         return result;
40     }
41
42     public static char[] ByteToBinString(byte b)
43     {
44         var result = new char[8];
45         for (int i = 0; i < 8; i++)
46         {
47             var temp = b & 128;
48             result[i] = temp == 0 ? '0' : '1';
49             b = (byte)(b << 1);
50         }
51         return result;
52      }
53 }
???????BitReader????4bit??byte[] buff= {35??12};?ж???????????????
????var reader = new BitReader(buff); //????????????0010001100001100
????var num1 = reader.ReadInt(4);   //????Position???4bit?int??Position???4bit??????2?????Position=4
????var num2 = reader.ReadInt(5??6);  //??????5bit??λ????6bit?int??Position???????????48?????Position=4
????var b = reader.ReadBool();  //????Position???1bit?bool??Position???1bit??????False?????Position=5
????0x02 ???????????д??
??????????д??????????????????????????BitWriter???????????д洢StringBuilder??????????????????д??????????????????????????????????????????bit??????д??????????StringBuilder?б??????????????????8bit????byte[]???????BitWriter???????????£?
1 public class BitWriter
2 {
3     public readonly StringBuilder BinString;
4
5     public BitWriter()
6     {
7         BinString = new StringBuilder();
8     }
9
10     public BitWriter(int bitLength)
11     {
12         var add = 8 - bitLength % 8;
13         BinString = new StringBuilder(bitLength + add);
14     }
15
16     public void WriteByte(byte b?? int bitLength=8)
17     {
18         var bin = Convert.ToString(b?? 2);
19         AppendBinString(bin?? bitLength);
20     }
21
22     public void WriteInt(int i?? int bitLength)
23     {
24         var bin = Convert.ToString(i?? 2);
25         AppendBinString(bin?? bitLength);
26     }
27
28     public void WriteChar7(char c)
29     {
30         var b = Convert.ToByte(c);
31         var bin = Convert.ToString(b?? 2);
32         AppendBinString(bin?? 7);
33     }
34
35     public byte[] GetBytes()
36     {
37         Check8();
38         var len = BinString.Length / 8;
39         var result = new byte[len];
40
41         for (int i = 0; i < len; i++)
42         {
43             var bits = BinString.ToString(i * 8?? 8);
44             result[i] = Convert.ToByte(bits?? 2);
45         }
46
47         return result;
48     }
49
50     public string GetBinString()
51     {
52         Check8();
53         return BinString.ToString();
54     }
55
56
57     private void AppendBinString(string bin?? int bitLength)
58     {
59         if (bin.Length > bitLength)
60             throw new Exception("len is too short");
61         var add = bitLength - bin.Length;
62         for (int i = 0; i < add; i++)
63         {
64             BinString.Append('0');
65         }
66         BinString.Append(bin);
67     }
68
69     private void Check8()
70     {
71         var add = 8 - BinString.Length % 8;
72         for (int i = 0; i < add; i++)
73         {
74             BinString.Append("0");
75         }
76     }
77 }
???????????????????
????var writer = new BitWriter();
????writer.Write(12??5);  //??12??5bitд???????????????????01100
????writer.Write(8??16);  //??8??16bitд???????????????????011000000000000001000
????var result = writer.GetBytes(); //8bit?????011000000000000001000000
????//???????[96??0??64]