c#使用编组进行数据包解析

时间:2013-08-14 08:15:36

标签: c# marshalling

我必须解析一个数据包,该数据包存储在ascii代码的byte []中        byte [] user = new byte [] {112,114,97,116,121,117,115,104,0,0,0,0,45,50,51,0} 其中前12个字节是servername,后4个字节是id。

通过浏览之前的stackoverflow帖子,我想出了这段代码

[StructLayout(LayoutKind.Explicit)]
struct packetrf
{
  public ulong servername
    {
        get
        {
            return (ulong)servername1 | ((ulong)servername2 << 8) | ((ulong)servername3 << 16) | ((ulong)servername4 << 24) | ((ulong)servername5 << 32) | ((ulong)servername6 << 40) | ((ulong)servername7 << 48) | ((ulong)servername8 << 56) | ((ulong)servername9 << 64) | ((ulong)servername10 << 72) | ((ulong)servername11 << 80) | ((ulong)servername12 << 88);
        }
    }

    [FieldOffset(0)]
    public byte servername1;

    [FieldOffset(1)]
    public byte servername2;

    [FieldOffset(2)]
    public byte servername3;

    [FieldOffset(3)]
    public byte servername4;

    [FieldOffset(4)]
    public byte servername5;

    [FieldOffset(5)]
    public byte servername6;

    [FieldOffset(6)]
    public byte servername7;

    [FieldOffset(7)]
    public byte servername8;

    [FieldOffset(8)]
    public byte servername9;

    [FieldOffset(9)]
    public byte servername10;

    [FieldOffset(10)]
    public byte servername11;

    [FieldOffset(11)]
    public byte servername12;

   [FieldOffset(12)]
    public Int32 imei_msn;
};

static private pack fromByte(byte[] arr)
{
    pack str = new pack();
    GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
    str = (pack)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(pack));
    handle.Free();
    return str;
}

static private void data_parser(byte[] pkt, int size, int indexno)
{
       packetrf data = fromByte(pkt);
        Console.WriteLine();
        Console.WriteLine(data.servername);
        Console.WriteLine(data.imei_msn);
        Console.ReadKey();
    }


public static void Main()
{
    byte[] user = new byte[] {112, 114, 97, 116, 121, 117, 115, 104, 0, 0, 0, 0, 49, 50, 51, 0}
    data_parser(user, 1, 2);
}

当我运行此代码时,我的输出为server name的7526488566770266736和imei_msn的13106,这可能是ascii-&gt; hex-&gt;十进制格式。现在我面临的问题是,如何将这些数字转换为某些意义的完整信息,例如第一个字符串显示名称,第二个字符串以十进制格式输入imei_msn?

4 个答案:

答案 0 :(得分:2)

尝试使用固定大小的数组(请在此处阅读https://stackoverflow.com/a/14629106/613130

[StructLayout(LayoutKind.Explicit)]
struct packetrf
{
    [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, SizeConst=12)]
    public byte[] servername;

    [FieldOffset(12)]
    public Int32 imei_msn;
};

然后从那里:

string serv = Encoding.ASCII.GetString(yourObject.servername).TrimEnd('\0');

或直接作为财产

public string serverName2
{
    get
    {
        return Encoding.ASCII.GetString(servername).TrimEnd('\0');
    }
}

请记住TrimEnd '\0'中所有byte[] ASCII

您确定编码是àèéìòù吗?尝试在名称中添加一些带重音的字母,例如UTF8 ......也许它是Default。或者它可能正在使用{{1}}编码。

答案 1 :(得分:0)

尝试将servername1 - servername12个字节添加到byte[12]数组中,然后使用

从中拉出一个字符串
byte[] array = GetBytesOfServerName();
string serverName = Encoding.ASCII.GetString(array);

答案 2 :(得分:0)

如果您对项目使用/ unsafe没问题,可以使用这样的固定缓冲区:

[StructLayout(LayoutKind.Explicit)]
unsafe struct packetrf
{
    [FieldOffset(0)]
    public fixed byte[12] servername;

    [FieldOffset(12)]
    public Int32 imei_msn;
}

答案 3 :(得分:0)

这应该有效:

string serverName = Encoding.ASCII.GetString(user.Take(12).ToArray());
string imeiMsn = Encoding.ASCII.GetString(user.Skip(12).ToArray());
Console.WriteLine(serverName);
Console.WriteLine(imeiMsn);