# 使用C#处理基于比特流的数据

## 0x01 比特流数据的读取

1、先把b表示为二进制字符串00100011

2、分别取其前后4bit转为数字，核心方法就是：

Convert.ToInt32("0010");

1、最简单的Convert.ToString(b,2)。不够8位就在高位用0补足。

2、也可以把byte分别与1,2,4,8 … 128做与运算，由低到高取出各位。

3、也可以把byte和32做与运算，然后把byte左移再次与128做与运算。

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; }

{
BinString = new StringBuilder(data.Length * 8);
for (int i = 0; i < data.Length;
{
BinString.Append(ByteToBinString(data[i]));
}
return BinString.ToString();
}

2 {
4     public int Position { get; set; }
5
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
17     {
18         var bin = BinString.ToString(offset, 8);
19         return Convert.ToByte(bin, 2);
20     }
21
23     {
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
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 }
View Code

## 0x02 比特流数据的写入

1 public class BitWriter
2 {
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 }
View Code

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]

## 0x03 7比特字符编码

public byte[] Encode(string text)
{
var len = text.Length * 7 + 24;

var writer = new BitWriter(len);
writer.WriteByte(2);
writer.WriteInt(text.Length, 16);

for (int i = 0; i < text.Length; i++)
{
var b = Convert.ToByte(text[i]);
writer.WriteByte(b, 7);
}

return writer.GetBytes();
}

public string Decode(byte[] data)
{
{
if (start == 2)
break;
}
var result = new StringBuilder(len);
for (int i = 0; i < len; i++)
{
var ch = Convert.ToChar(b);
result.Append(ch);
}

return result.ToString();
}

## 0x04 6比特字符编码

public byte[] Encode(string text)
{
text = text.ToUpper();
var len = text.Length * 6 + 24;

var writer = new BitWriter(len);
writer.WriteByte(2);
writer.WriteInt(text.Length, 16);

for (int i = 0; i < text.Length; i++)
{
var index = GetChar6Index(text[i]);
writer.WriteInt(index, 6);
}

return writer.GetBytes();

}

private int GetChar6Index(char c)
{
for (int i = 0; i < 64; i++)
{
if (Dict.Custom[i] == c)
return i;
}
return 10; //return *
}

public string Decode(byte[] data)
{
{
if (start == 2)
break;
}
var result = new StringBuilder(len);
for (int i = 0; i < len; i++)
{
var ch = Dict.Custom[index];
result.Append(ch);
}

return result.ToString();
}

## 0x05 写在最后

posted @ 2016-10-13 17:45 durow 阅读(...) 评论(...) 编辑 收藏