# 数据打包DataPacker

如果是定长数据就直接压入buffer,不是定长数据压入一个长度再压入数据。

![](/files/-Ml5pSFsyGrpfe8q35SY)

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xxx.Core
{
    class DataPacker
    {
        byte[] buffer = new byte[] { };
        int size = 0;
        public DataPacker(byte[] data)
        {
            size = BitConverter.ToInt32(data[..3]);
            buffer = data[4..];
        }
        public DataPacker()
        {
        }
        public byte[] sub(int start,int end)
        {
            return buffer[start..end];
        }
        public void push(int data)
        {
            var dataBytes = BitConverter.GetBytes(data);
            buffer = Utils.Combine(buffer, dataBytes);
        }
        public void push(short data)
        {
            var dataBytes = BitConverter.GetBytes(data);
            buffer = Utils.Combine(buffer, dataBytes);
        }
        public void push(string data)
        {
            var sizeBytes = BitConverter.GetBytes(data.Length + 1);
            var dataBytes = Encoding.ASCII.GetBytes(data);
            buffer = Utils.Combine(buffer, sizeBytes, dataBytes, new byte[] { 0x00 });
        }
        public void push(byte data)
        {
            buffer = Utils.Combine(buffer, new byte[] { data});
        }

        public void push(byte[] data)
        {
            var dataBytes = BitConverter.GetBytes(data.Length);
            buffer = Utils.Combine(buffer, dataBytes, data);
        }
        public byte[] GetBuffer()
        {
            var dataBytes = BitConverter.GetBytes(buffer.Length+4);
            return (byte[])Utils.Combine(dataBytes, buffer).Clone();
        }

    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://idiotc4t.com/weapon-design/c2-manuscript/shu-ju-da-bao-fang-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
