Giter Club home page Giter Club logo

bixet's Introduction

Bixet:应用层数据解析工具集


本文内容


项目描述

项目介绍

       Bixet是一组工具集合,为用户提供对以字节流或比特流形式表示的数据进行操作的若干功能。不同的功能被实现为不同的类以供用户使用。本项目计划支持的功能及其最新实现版本如下所示:

名称 功能 最新实现版本
Bixet程序集 项目生成的程序集 v0.1.0
BUtil 对字节流或比特流进行操作的工具方法集 v0.2.0
BReader 从数据指定字节位置/比特位置处依照指定字节序/比特序读取指定长度的数值或字符串 v0.4.0
BWriter 向数据指定字节位置或比特位置处依照指定字节序/比特序写入指定长度的数值或字符串 v0.4.4
BTemplete & BBlock & BVariable(均为暂定名) 将数据格式描述为形式化模板 开发中
BResolver 依照数据格式模板对数据进行自动化的解析与填充 待实现

项目依赖

运行环境

.Net Framework 4.7.2及以上版本

项目依赖命名空间

1. Newtonsoft.Json
2. FluentAssertions
3. Microsoft.VisualStudio.TestTools.UnitTesting

使用方法

       在.Net项目中引用编译后的Bixet.dll,并在自己程序中需要的位置添加对命名空间的使用。


数据内存布局

       Bixet中默认的数据的字节排布顺序与字节内部的比特排布顺序如下图所示:

内存布局

       即对于下述代码所描述的数据:

byte[] data = new byte[]{ 0xEF, 0x12, 0x34, 0x56, 0x78, 0xFE};

       Bixet内全部工具将认为数据的字节1至字节4依次为0x12, 0x34, 0x56, 0x78;数据的比特17至比特23分别为0、1、0、1、1、0、0。

       若用户数据的内存布局与Bixet使用布局不一致,用户可使用Bixet所提供的工具方法对数据内存布局进行转换。


BUtil类

命名空间:Bixet
程序集:Bixet.dll

对比特数据或字节数据进行操作的工具方法集合静态类。
public static class BUtil

BUtil类内常量

常量名 说明
version BUtil的版本号

BUtil方法

方法签名 说明
public static void ReverseByteEndian(byte[] bytes) 逆转byte中全部字节的字节序
public static void ReverseByteEndian(byte[] bytes, int begin, int end) 逆转bytes的第begin个字节至第end个字节的字节序
public static void ReverseBitEndian(byte[] bytes) 逆转bytes的每个字节的比特序
public static void ReverseBitEndian(System.Collections.BitArray bits) 按照8比特1组逆转bits中全部各组比特的比特序
注:bits的长度必须为8的整数倍
public static void ReverseBitEndian(System.Collections.BitArray bits, int begin, int end) 按照8比特1组逆转bits中从第begin至第end个比特中的全部各组比特的比特序
_注:end - begin 的长度必须为8的整数倍
public static void ReverseBitsOrder(byte[] bytes) 逆转bytes全部比特的顺序
public static void ReverseBitsOrder(System.Collections.BitArray bits) 逆转bits全部比特的顺序
public static void ReverseBitsOrder(System.Collections.BitArray bits, int begin, int end) 逆转bits的第begin个比特至第end个比特的顺序

枚举类

命名空间:Bixet
程序集:Bixet.dll

用于描述数据及数据结构性质的枚举类。
类名 说明 枚举值
Endian 字节或比特的排布顺序 BigEndian: 大端序
SmallEndian:小端序

BReader类

命名空间:Bixet
程序集:Bixet.dll

从字节数组的指定位置中读取数值或字符串。
public class BReader

BReader示例

using System;
using Bixet;

class Program
{
    static void Main()
    {
        byte[] data = new byte[] { 
            0x01, 0x12, 0x34, 0x56, 0x78, 0b10101010, 0b11100100, 
            (byte)'B', (byte)'i', (byte)'x', (byte)'e', (byte)'t'
        };
        BReader br = new BReader(data);
        byte aByte = br.ReadValueByByteIndex<byte>(0, 1);
        Console.WriteLine($"Read a byte from the 0th byte :" +
            $"0x{Convert.ToString(aByte, 16).PadLeft(2, '0')}");
        int aInt = br.ReadValueByByteIndex<int>(1, 4);
        Console.WriteLine($"Read an integer from the 1th-4th bytes: " +
            $"0x{Convert.ToString(aInt, 16).PadLeft(8, '0')}");
        for (int i = 0; i < 8; ++i)
        {
            Console.WriteLine($"Read a bit from the {i}th bit of the 5th byte: " +
                $"{br.ReadValueByBitIndex<bool>(5, i, 1)}");
            Console.WriteLine($"The above line is equivalent to read a bit from the {40 + i}th bit: " +
                $"{br.ReadValueByBitIndex<bool>(40 + i, 1)}");
        }
        for (int i = 0; i < 4; i++)
        {
            Console.WriteLine($"Read a byte from the {2 * i}-{2 * i + 1}th bits of the 6th byte :" +
                $"0x{Convert.ToString(br.ReadValueByBitIndex<byte>(6, 2 * i, 2), 16).PadLeft(2, '0')}");
            Console.WriteLine($"The above line is equivalent to read a byte from the {48 + 2 * i}-{48 + 2 * i + 1} bits: " +
                $"0x{Convert.ToString(br.ReadValueByBitIndex<byte>(48 + 2 * i, 2), 16).PadLeft(2, '0')}");
        }
        Console.WriteLine($"Read a byte from the 8 bits beginning at the 4th bit of 5th byte: " +
            $"0x{Convert.ToString(br.ReadValueByBitIndex<byte>(5, 4, 8), 16).PadLeft(2, '0')}");
        Console.WriteLine($"The above line is equivalent to read a byte from the 8 bits beginning at the 44th bit: " +
            $"0x{Convert.ToString(br.ReadValueByBitIndex<byte>(44, 8), 16).PadLeft(2, '0')}");
        Console.WriteLine($"Read a 5 bytes long string with length 5 from the 7th byte :" +
            $"{br.ReadStringByByteIndex(7, 5)}");
        Console.WriteLine($"The above line is equivalent to read a 40 bits long string from the 0th bit of the 7th byte: " +
            $"{br.ReadStringByByteIndex(7, 5)}");
        Console.WriteLine($"The above line is equivalent to read a 40 bits long string from the 56th bit: " +
            $"{br.ReadStringByByteIndex(7, 5)}");
        Console.ReadKey();

        /* Output:
        Read a byte from the 0th byte: 0x01
        Read an integer from the 1th-4th bytes: 0x12345678
        Read a bit from the 0th bit of the 5th byte: False
        The above line is equivalent to read a bit from the 40th bit: False
        Read a bit from the 1th bit of the 5th byte: True
        The above line is equivalent to read a bit from the 41th bit: True
        Read a bit from the 2th bit of the 5th byte: False
        The above line is equivalent to read a bit from the 42th bit: False
        Read a bit from the 3th bit of the 5th byte: True
        The above line is equivalent to read a bit from the 43th bit: True
        Read a bit from the 4th bit of the 5th byte: False
        The above line is equivalent to read a bit from the 44th bit: False
        Read a bit from the 5th bit of the 5th byte: True
        The above line is equivalent to read a bit from the 45th bit: True
        Read a bit from the 6th bit of the 5th byte: False
        The above line is equivalent to read a bit from the 46th bit: False
        Read a bit from the 7th bit of the 5th byte: True
        The above line is equivalent to read a bit from the 47th bit: True
        Read a byte from the 0-1th bits of the 6th byte: 0x00
        The above line is equivalent to read a byte from the 48-49 bits:  0x00
        Read a byte from the 2-3th bits of the 6th byte: 0x01
        The above line is equivalent to read a byte from the 50-51 bits:  0x01
        Read a byte from the 4-5th bits of the 6th byte: 0x02
        The above line is equivalent to read a byte from the 52-53 bits:  0x02
        Read a byte from the 6-7th bits of the 6th byte: 0x03
        The above line is equivalent to read a byte from the 54-55 bits:  0x03
        Read a byte from the 8 bits beginning at the 4th bit of 5th byte: 0x4a
        The above line is equivalent to read a byte from the 8 bits beginning at the 44th bit: 0x4a
        Read a 5 bytes long string with length 5 from the 7th byte: Bixet
        The above line is equivalent to read a 40 bits long string from the 0th bit of the 7th byte: Bixet
        The above line is equivalent to read a 40 bits long string from the 56th bit: Bixet
        */
    }
}

BReader构造函数

构造函数签名 说明
public BReader(byte[] bytes) bytes的全部字节作为可读取数据
public BReader(byte[] bytes, int length) bytes的前length个字节作为可读取数据
public BReader(byte[] bytes, int offset, int length) bytes中从offset个字节开始的length个字节作为可读取数据

BReader类内常量

常量名 说明
public const string version BReader的版本号
public const int maxBytesLength 单次可读取的最大字节数
public const int maxBitsLength 单次可读取的最大比特数

BReader属性

属性名 说明
public int BytesCount 可读取数据字节数
public int BitsCount 可读取数据比特数

BReader运算符重载

运算符签名 说明
public byte this[int i] 获取可读取数据的第i个字节
public byte this[int i, int j] 获取可读取数据的第i个字节的第j个比特(结果以字节表示)

BReader方法

方法签名 说明
public byte[] GetRawBytes(int beginIndex, int length) 获取可读取数据从第beginIndex个字节处开始的length个字节
public System.Collections.BitArray GetRawBits(int beginIndex, int length) 获取可读取数据从第beginIndex个比特处开始的length个比特
public System.Collections.BitArray GetRawBits(int byteIndex, int bitIndex, int length) 获取可读取数据从第byteIndex个字节的第bitIndex个比特处开始的length个比特
public T ReadValueByByteIndex<T>(int beginIndex, int length, Endian byteEndian = Endian.BigEndian) 将可读取数据的从第beginIndex个字节开始的length个字节按照byteEndian的字节序读取为_T_类型的数值
注:支持读取的数值的字节数不能超过maxBytesLength,支持读取的类型为:sbyte, byte, short, ushort, int, uint, long, ulong
public string ReadStringByByteIndex(int beginIndex, int length, Endian byteEndian = Endian.BigEndian, System.Text.Encoding encoding = null) 将可读取数据的从第beginIndex个字节开始的length个字节按照byteEndian的字节序以encoding编码方式读取为字符串
注:若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码
public T ReadValueByBitIndex<T>(int beginIndex, int length, Endian bitEndian = Endian.SmallEndian) 将可读取数据的从第beginIndex个比特开始的length个比特按照bitEndian的比特序读取为_T_类型的数值
注:支持读取的数值的比特数不能超过maxBitsLength,支持读取的类型为:bool, sbyte, byte, short, ushort, int, uint, long, ulong
public T ReadValueByBitIndex<T>(int byteIndex, int bitIndex, int length, Endian bitEndian = Endian.SmallEndian) 将可读取数据的从第byteIndex个字节的第bitIndex个比特开始的length个比特按照bitEndian的比特序读取为_T_类型的数值
注:支持读取的数值的最大比特数与支持类型与前一方法相同
public string ReadStringByBitIndex(int beginIndex, int length, Endian bitEndian = Endian.SmallEndian, System.Text.Encoding encoding = null) 将可读取数据的从第beginIndex个比特开始的length个比特按照bitEndian的比特序以encoding编码方式读取为字符串
注:输入的length参数长度必须为8的整数倍;若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码
public string ReadStringByBitIndex(int byteIndex, int bitIndex, int length, Endian bitEndian = Endian.SmallEndian, System.Text.Encoding encoding = null) 将可读取数据的从第byteIndex个字节的第bitIndex个比特开始的length个比特按照bitEndian的比特序以encoding编码方式读取为字符串
注:输入的length参数长度必须为8的整数倍;若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码

BWriter类

命名空间:Bixet
程序集:Bixet.dll

生成指定长度的字节数组,并向其写入数值或字符串。
public class BWriter

BWriter示例

using System;
using Bixet;

class Program
{
    static void Main()
    {
        Console.WriteLine("Create a BWriter with length of 29 bytes");
        BWriter bw = new BWriter(29);
        Console.WriteLine("Write 4 bytes 0x12345678 starting at the 0th byte");
        bw.WriteValueByByteIndex<int>(0, 0x12345678, 4);
        Console.WriteLine("Write 32 bits 0x12345678 starting at the 32nd bit");
        bw.WriteValueByBitIndex<int>(32, 0x12345678, 32);
        Console.WriteLine("Write 32 bits 0x12345678 starting at the 0th bit of the 8th byte");
        bw.WriteValueByBitIndex<long>(8, 0, 0x12345678, 32);
        Console.WriteLine("Write 1 * 8 bits starting at the 0th bit of the 12th byte");
        for (int i = 0; i < 8; ++i)
        {
            bw.WriteValueByBitIndex<bool>(12, i, i % 2 == 1, 1);
        }
        Console.WriteLine("Write 2 * 4 bits starting at the 104th bit");
        for (short i = 0; i < 4; ++i)
        {
            bw.WriteValueByBitIndex<short>(104 + 2 * i, i, 2);
        }
        string s = "Bixet";
        Console.WriteLine("Write the 5-bytes-long string starting at the 14th byte");
        bw.WriteStringByByteIndex(14, s, 5);
        Console.WriteLine("Write the 40-bits-long string starting at the 152th bit");
        bw.WriteStringByBitIndex(152, s, 40);
        Console.WriteLine("Write the 40-bits-long string starting at the 0th bit of the 24th byte");
        bw.WriteStringByBitIndex(24, 0, s, 40);
        byte[] bytes = bw.GetData();
        Console.WriteLine($"Length of the generated bytes: {bytes.Length}");
        Console.Write("The first 4 bytes of the generated bytes: ");
        for (int i = 0; i < 4; ++i) Console.Write($"0x{Convert.ToString(bytes[i], 16)} ");
        Console.WriteLine();
        Console.Write("The 4th-7th bytes of the generated bytes: ");
        for (int i = 4; i < 8; ++i) Console.Write($"0x{Convert.ToString(bytes[i], 16)} ");
        Console.WriteLine();
        Console.Write("The 8th-11th bytes of the generated bytes: ");
        for (int i = 8; i < 12; ++i) Console.Write($"0x{Convert.ToString(bytes[i], 16)} ");
        Console.WriteLine();
        Console.WriteLine($"The 12th byte of the generated bytes: 0b{Convert.ToString(bytes[12], 2)}");
        Console.WriteLine($"The 13th byte of the generated bytes: 0b{Convert.ToString(bytes[13], 2)}");
        Console.WriteLine($"Decoded string from 14th-18th bytes of the generated bytes: {System.Text.Encoding.Default.GetString(bytes, 14, 5)}");
        Console.WriteLine($"Decoded string from 19th-23th bytes of the generated bytes: {System.Text.Encoding.Default.GetString(bytes, 19, 5)}");
        Console.WriteLine($"Decoded string from 24th-28th bytes of the generated bytes: {System.Text.Encoding.Default.GetString(bytes, 24, 5)}");
        Console.ReadKey();

        /* Output:
        Create a BWriter with length of 29 bytes
        Write 4 bytes 0x12345678 starting at the 0th byte
        Write 32 bits 0x12345678 starting at the 32nd bit
        Write 32 bits 0x12345678 starting at the 0th bit of the 8th byte
        Write 1 * 8 bits starting at the 0th bit of the 12th byte
        Write 2 * 4 bits starting at the 104th bit
        Write the 5-bytes-long string starting at the 14th byte
        Write the 40-bits-long string starting at the 152th bit
        Write the 40-bits-long string starting at the 0th bit of the 24th byte
        Length of the generated bytes: 29
        The first 4 bytes of the generated bytes: 0x12 0x34 0x56 0x78
        The 4th-7th bytes of the generated bytes: 0x78 0x56 0x34 0x12
        The 8th-11th bytes of the generated bytes: 0x78 0x56 0x34 0x12
        The 12th byte of the generated bytes: 0b10101010
        The 13th byte of the generated bytes: 0b11100100
        Decoded string from 14th-18th bytes of the generated bytes: Bixet
        Decoded string from 19th-23th bytes of the generated bytes: Bixet
        Decoded string from 24th-28th bytes of the generated bytes: Bixet
        */
    }
}

BWriter构造函数

构造函数签名 说明
public BWriter(int byteLength) 创建byteLength字节的可写入数据
public BWriter(byte[] data) 创建字节数与data大小相同的可写入数据,并将data内容作为可写入数据的初始值

BWriter类内常量

常量名 说明
public const string version BWriter的版本号
public const int maxBytesLength 单次可写入的最大字节数
public const int maxBitsLength 单次可写入与的最大比特数

BWriter属性

属性名 说明
public int BytesCount 可写入数据字节数
public int BitsCount 可写入数据比特数

BWriter运算符重载

运算符签名 说明
public byte this[int i] 写入第i个字节
public byte this[int i, int j] 写入第i个字节的第j个比特(输入比特值以字节表示)

BWriter方法

方法签名 说明
public void SetRawBytes(int destIndex, byte[] bytes) 向可写入数据的第destIndex字节位置写入bytes的全部字节
public void SetRawBytes(int destIndex, byte[] bytes, uint offset, int length) 向可写入数据的第destIndex字节位置写入bytes的从offset位置开始的length个字节
public void SetRawBits(int destIndex, System.Collections.BitArray bits) 向可写入数据的第destIndex个比特位置写入bits的全部比特
public void SetRawBits(int destIndex, System.Collections.BitArray bits, int offset, int length) 向可写入数据的第destIndex个比特位置写入bitsoffset位置开始的length个比特
public void SetRawBits(int byteIndex, int bitIndex, System.Collections.BitArray bits) 向可写入数据的第byteIndex个字节的bitIndex个比特位置写入bits的全部比特
public void SetRawBits(int byteIndex, int bitIndex, System.Collections.BitArray bits, int offset, int length) 向可写入数据的第byteIndex个字节的bitIndex个比特位置写入bitsoffset位置开始的length个比特
public void WriteValueByByteIndex<T>(int beginIndex, T value, Endian byteEndian = Endian.BigEndian) 向可写入数据的第beginIndex个字节处按照byteEndian的字节序写入类型为T的数据value
注:写入的字节数为T所占用的字节数。支持写入的类型为:sbyte, byte, short, ushort, int, uint, long, ulong
public void WriteValueByByteIndex<T>(int beginIndex, T value, int length, Endian byteEndian = Endian.BigEndian) 向可写入数据的第beginIndex个字节处按照byteEndian的字节序写入类型为T的数据value的前length个字节
注:支持写入的类型为:sbyte, byte, short, ushort, int, uint, long, ulong
public void WriteValueByBitIndex<T>(int beginIndex, T value, int length, Endian bitEndian = Endian.SmallEndian) 向可写入数据的第beginIndex个比特处按照bitEndian的比特序写入类型为T的数据value的前length个比特
注:支持写入的类型为:bool, sbyte, byte, short, ushort, int, uint, long, ulong
public void WriteValueByBitIndex<T>(int byteIndex, int bitIndex, T value, int length, Endian bitEndian = Endian.SmallEndian) 向可写入数据的第byteIndex个字节的第bitIndex个比特处按照bitEndian的比特序写入类型为T的数据value的前length个比特
注:支持写入的类型为:bool, sbyte, byte, short, ushort, int, uint, long, ulong
public void WriteStringByByteIndex(int beginIndex, string s, int length, Endian byteEndian = Endian.BigEndian, System.Text.Encoding encoding = null) 向可写入数据的第beginIndex个字节位置按照encoding的编码方式写入编码后长度为length字节的字符串s
注:若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码
public void WriteStringByBitIndex(int beginIndex, string s, int length, Endian bitEndian = Endian.SmallEndian, System.Text.Encoding encoding = null) 向可写入数据的第beginIndex个比特位置按照encoding的编码方式写入编码后长度为length比特的字符串s
注:若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码
public void WriteStringByBitIndex(int byteIndex, int bitIndex, string s, int length, Endian bitEndian = Endian.SmallEndian, System.Text.Encoding encoding = null) 向可写入数据的第byteIndex个字节的第bitIndex个比特位置按照encoding的编码方式写入编码后长度为length比特的字符串s
注:若输入的encoding参数为null,将使用系统默认的编码方式System.Text.Encoding.Default对数据进行解码
public byte[] GetData(byte[] res = null) 将可写入数据保存至res
注:若输入的参数resnull,将自动创建并返回新的字节数组,否则将使用输入数组

文档剩余部分待完成……

bixet's People

Contributors

gbamqzkdyg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.