ネットワークプロトコルのエンコーディング

 

ネットワークプロトコルは、ビット識別子によって行うことができます。

クライアント(C#)パケット開梱

1.プロトコル番号の分割

4 0000を代表する16のキー操作1、

4 0000を代表する16の二次アクション1、

操作の256種類の合計を乗じ

5 - 次に、5ビットの演算によりバイトに、すなわち、01010101、INTすなわち85

 

2ビットのデータ・バイトに封入

2.1 使用 BitArray 

C#のドットアレイ(BitArray)|初心者チュートリアル

2.2 int型とBitArray Huzhuan

BitArrayストレージはれるローからハイに配置され、そのような85として、バイナリ01010101であります

現存するBitArrayは真偽真偽真偽真偽であります

パブリック 静的 INT BitToInt(BitArrayビット){
     int型のRES = 0 

    以下のためにint型私= 0 ;私<bit.Count、私は++ 
    {
        RES =ビット[i]は?RES +(1 << I):RES。
    }

    リターンのres;
}

用例 1

バイト [] = { 60 }。
BitArrayビット = 新しいBitArray(A)。
プリント(a.Length)。
プリント(bit.Length)。
プリント(BitPack.BitToInt(ビット))。

プリント

実施例2の、BitArrayをアンパックに[]バイトであるかもしれ

バイト [] = { 60255 }。
BitArrayビット = 新しいBitArray(A)。
プリント(a.Length)。
プリント(bit.Length)。
プリント(BitPack.BitToInt(ビット))。

プリント

BitArray 8以上の長さのない分裂がないので、彼は数および最大2つは、に対処する必要がある、見つけることができます。

注:最大記憶された値255 = 11111111

2.3スプリットBitArray

パブリック 静的リスト< INT > SplitMixedInt(BitArrayビット、int型の量、リスト< INT > 長さ){
     // (bit.Length> 8)の場合
     //     NULLを返します。

    一覧 < 整数 >ファイナル= 新しいリスト< 整数 > ();

    int型 lengthSum = 0 ;
    以下のためにint型私= 0 ;私はlength.Countを<;私は++ 
    {
        final.Add(BitToInt(ビット、lengthSum、長さ[I]))。
        lengthSum + = 長さ[I]。
    }

    final.Reverse();

    返す最終;
}

用例 4

バイト [] mixedInt = { 85 }。
BitArrayビット = 新しいBitArray(mixedInt)。

INT [] =のgetInt BitPack.SplitMixedInt(ビット、44 );
プリント(のgetInt [ 0 ] + " " +のgetInt [ 1 ])。

INT newInt = BitPack.BitToInt(ビット、04 )。
プリント(newInt)。

プリント

 

次はint型BitArrayをプッシュする方法を検討し、唯一の目標数字を占めている必要があります

int型は、任意の長さの2.4 BitArray前進

現在進バイナリ文字列、BitArrayセグメンテーション割当に、文字列から転送されたことが企図。

C#バイナリ変換(バイナリ、16進数、10進数システムの変換) - 製氷皿 - ブログパーク

int型の D = 10 ;

// 小数バイナリストリング転送 
 Console.WriteLineを(Convert.ToString(D、2 ));
 // 出力:1010

 

パブリック 静的 BitArrayパック(BitArrayビット、int型の数、INT startPos、int型の長さ){
     場合(ビット== NULL 
    {
        リターン ヌル
    }

    もし(bit.Length <startPos + 長さ)
    {
        リターン ヌル
    }

    ストリング bNumStr = Convert.ToString(数2 

    もし(bNumStr.Length> 長)
    {
        リターン ヌル
    }

    以下のためにint型 i = 0 ; iは長さ<; iは++ 
    {
        INT IDX = bNumStr.Length - 1 - I;
        もし(IDX < 0 
            bit.Set(startPos + I、   );
        
            bit.Set(startPos + I、bNumStr [IDX] == ' 1 ');
    }
    リターンビット。
}

用例 5

BitArrayビット= 新しい BitArray(8 )。
BitPack.Pack(ビット、203 )。
BitPack.Pack(ビット、1335 )。
foreachのブール B におけるビット)
    プリント(B)。

プリント

2.5 BitArrayバイト[]に変換されます

C#で[]バイトをBitArrayを変換します

パブリック 静的 バイト[] ToByteArray(BitArrayビット){
     場合(ビット== NULL 
    {
        リターン ヌル
    }

    int numBytes = bits.Count / 8;
    if (bits.Count % 8 != 0) numBytes++;

    byte[] bytes = new byte[numBytes];
    int byteIndex = 0, bitIndex = 0;

    for (int i = 0; i < bits.Count; i++)
    {
        if (bits[i])
            bytes[byteIndex] |= (byte)(1 << (7 - bitIndex));

        bitIndex++;
        if (bitIndex == 8)
        {
            bitIndex = 0;
            byteIndex++;
        }
    }

    return bytes;
}

用例 6

BitArray bit = new BitArray(8);
BitPack.Pack(bit, 2, 0, 3);
BitPack.Pack(bit, 13, 3, 5);
byte[] byteArray = BitPack.ToByteArray(bit);
print(byteArray.Length);
foreach (object obj in byteArray)
    print(obj.ToString());

打印

 

3. 服务器(Python3)封包拆包

由于 C# 中的 BitArray 是左起为低位,所以跟 Python 解出来的是不一样的结果。

BitArray bit = new BitArray(8);
BitPack.Pack(bit, 2, 0, 4);
BitPack.Pack(bit, 13, 4, 4);
byte[] byteArray = BitPack.ToByteArray(bit);
print(BitPack.BitToInt(bit, 0, 4));
print(BitPack.BitToInt(bit, 4, 4));

string s = "";
foreach (bool b in bit)
    s += b + " ";
print(s);

print(BitPack.BitToInt(bit, 0, 8));

服务端这边,则默认是右边为低位,其实影响不大

def ByteToBit(bytes):
    bits = []
    for i in bytes:
        bits.append(i)
    return bits

print('收到:', datar)
print('转码bit:', ByteToBit(data))

3.1 将 byte[] 转换成位数组

def ByteToBits(self, bytes):
    bits = []
    for byte in bytes:
        n0 = 1 if (byte & 0x01) == 0x01 else 0;
        n1 = 1 if (byte & 0x02) == 0x02 else 0;
        n2 = 1 if (byte & 0x04) == 0x04 else 0;
        n3 = 1 if (byte & 0x08) == 0x08 else 0;
        n4 = 1 if (byte & 0x10) == 0x10 else 0;
        n5 = 1 if (byte & 0x20) == 0x20 else 0;
        n6 = 1 if (byte & 0x40) == 0x40 else 0;
        n7 = 1 if (byte & 0x80) == 0x80 else 0;
        bits.append(n7);
        bits.append(n6);
        bits.append(n5);
        bits.append(n4);
        bits.append(n3);
        bits.append(n2);
        bits.append(n1);
        bits.append(n0);
    return bits

用例 7 

// 客户端封包
BitArray bit = new BitArray(12);
BitPack.Pack(bit, 2, 0, 4);
BitPack.Pack(bit, 13, 4, 4);
BitPack.Pack(bit, 13, 8, 4);
byte[] byteArray = BitPack.ToByteArray(bit);
print(BitPack.BitToInt(bit, 0, 4));
print(BitPack.BitToInt(bit, 4, 4));

string s = "";
foreach (bool b in bit)
    s += b + " ";
print(s);

print(BitPack.BitToInt(bit, 0, 8));

 

 

おすすめ

転載: www.cnblogs.com/xuuold/p/11257471.html