欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#如何利用結(jié)構(gòu)體對固定格式數(shù)據(jù)進行解析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文為大家分享了C#利用結(jié)構(gòu)體解析固定格式數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

制定了一個通訊協(xié)議,然后其數(shù)據(jù)部分有如下格式。

第三列代表的是字節(jié)數(shù),第4列是數(shù)據(jù)類型。

當傳輸或者收到一個byte數(shù)組的時候(下面Hex數(shù)據(jù)),按照對應(yīng)格式進行解析,解析方法有很多種,網(wǎng)上看到了一種方式是以結(jié)構(gòu)體的方式來解析的,類似C/C++方式。

Hex數(shù)據(jù):01 01 00 00 10 44 65 76 69 63 65 20 4E 61 6D 65 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 31 2E 30 2E 30 00 00 00 00 00 00 00 00 00 00 00 41 42 43 31 32 33 34 35 36 37 00 00 00 00 00 00 56 31 2E 30 2E 30 00 00 00 00 00 00 00 00 00 00 32 30 31 38 2F 31 2F 32 32 00 00 00 00 00 00 00

定義一個結(jié)構(gòu)體:

using System.Runtime.InteropServices;
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  public struct InfoStruct
  {
    [MarshalAs(UnmanagedType.U1, SizeConst = 1)]
    public byte SlotNum;
    [MarshalAs(UnmanagedType.U4,SizeConst =4)]
    public UInt32 ModuleID;
    [MarshalAs(UnmanagedType.ByValArray,SizeConst =32)]
    public char[] DeviceName;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public char[] HardwareNum;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public char[] HardwareVersion;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public char[] SoftwareVersion;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public char[] SoftwareDate;
  }

再寫一個輔助解析的靜態(tài)幫助類,該類提供將結(jié)構(gòu)體轉(zhuǎn)成byte數(shù)組和byte數(shù)組轉(zhuǎn)成結(jié)構(gòu)體功能,我在原來的方法上面添加了泛型,功能不變:

public static class StructHelper
  {
    /// <summary>
    /// byte數(shù)組轉(zhuǎn)目標結(jié)構(gòu)體
    /// </summary>
    /// <param name="bytes">byte數(shù)組</param>
    /// <param name="type">目標結(jié)構(gòu)體類型</param>
    /// <returns>目標結(jié)構(gòu)體</returns>
    public static T ByteToStuct<T>(byte[] DataBuff_) where T:struct
    {
      Type t = typeof(T);
      //得到結(jié)構(gòu)體大小
      int size = Marshal.SizeOf(t);
      //數(shù)組長度小于結(jié)構(gòu)體大小
      if (size > DataBuff_.Length)
      {
        return default(T);
      }

      //分配結(jié)構(gòu)體大小的內(nèi)存空間
      IntPtr structPtr = Marshal.AllocHGlobal(size);
      //將byte數(shù)組cpoy到分配好的內(nèi)存空間內(nèi)
      Marshal.Copy(DataBuff_, 0, structPtr, size);
      //將內(nèi)存空間轉(zhuǎn)換為目標結(jié)構(gòu)體
      T obj = (T)Marshal.PtrToStructure(structPtr, t);
      //釋放內(nèi)存空間
      Marshal.FreeHGlobal(structPtr);
      return obj;
    }
    /// <summary>
    /// 結(jié)構(gòu)體轉(zhuǎn)byte數(shù)組
    /// </summary>
    /// <param name="objstuct">結(jié)構(gòu)體</param>
    /// <returns>byte數(shù)組</returns>
    public static byte[] StuctToByte(object objstuct)
    {
      //得到結(jié)構(gòu)體大小
      int size = Marshal.SizeOf(objstuct);
      //創(chuàng)建byte數(shù)組
      byte[] bytes = new byte[size];
      //分配結(jié)構(gòu)體大小的空間
      IntPtr structPtr = Marshal.AllocHGlobal(size);
      //將結(jié)構(gòu)體copy到分配好的內(nèi)存空間內(nèi)
      Marshal.StructureToPtr(objstuct, structPtr, false);
      //從內(nèi)存空間copy到byte數(shù)組
      Marshal.Copy(structPtr, bytes, 0, size);
      //釋放內(nèi)存空間
      Marshal.FreeHGlobal(structPtr);
      return bytes;
    }
  }

好了現(xiàn)在結(jié)構(gòu)體有了,轉(zhuǎn)換方法也有了那么就來使用一下吧!先將結(jié)構(gòu)體轉(zhuǎn)為byte數(shù)組,然后再還原結(jié)構(gòu)體試試:

static void Main(string[] args)
    {
      try
      {
        InfoStruct Info;
        Info.HardwareNum = "1.0.0".ToCharArray();
        Info.HardwareVersion = "ABC1234567".ToCharArray();
        Info.DeviceName = "Device Name1".ToCharArray();
        Info.ModuleID = 0x10000001;
        Info.SlotNum = 1;
        Info.SoftwareDate = "2018/1/22".ToCharArray();
        Info.SoftwareVersion = "V1.0.0".ToCharArray();
        var b = StructHelper.StuctToByte(Info);
        Console.WriteLine("Struct length:"+b.Length);
        Console.WriteLine("Hex:"+ByteToolsHelper.ByteArrayToHexString(b," "));
        var s = StructHelper.ByteToStuct<InfoStruct>(b);
        Console.WriteLine("Name:"+s.DeviceName.GetString());
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
      Console.ReadKey();

    }

其中ByteToolsHelper.ByteArrayToHexString是我封裝的一個函數(shù),將byte數(shù)組轉(zhuǎn)為Hex字符串,方便顯示和調(diào)試可以不用管。

然后調(diào)試運行得到結(jié)果:

我擦,這是什么情況?什么叫“未能封送類型,因為嵌入數(shù)組實例的長度與布局中聲明的長度不匹配?????”

調(diào)試一下就可以發(fā)現(xiàn)實際結(jié)構(gòu)體標記的SizeConst和ToCharArray()函數(shù)得到的長度并不一樣,字符串通過ToCharArray()得到的長度不足導(dǎo)致出現(xiàn)這個異常。

既然是長度不足,那么就想辦法補足吧。

寫個靜態(tài)擴展方法,包含上面的GetString擴展方法:

public static char[] GetFixLengthChar(this string s,int length)
    {
      char[] chaVal = new char[length];
      Array.Copy(s.PadRight(length, '\0').ToCharArray(), chaVal, length);
      return chaVal;
    }
    public static string GetString(this char[] cc)
    {
      return GetString(cc,true);
    }
    public static string GetString(this char[] cc,bool isTrimEnd)
    {
      if (isTrimEnd)
      {
        return new string(cc).TrimEnd('\0');
      }
      else
      {
        return new string(cc);
      }
    }

GetFixLengthChar是將字符串轉(zhuǎn)為固定長度char數(shù)組,GetString是從char數(shù)組轉(zhuǎn)為字符串,因為有'\0'可以用TrimEnd函數(shù)去掉,這樣字符串后面就不會有一排空的了。

我們再試試結(jié)果:

沒問題!成功的轉(zhuǎn)換和還原了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:c#使用IMap收取163郵件的方法示例

欄    目:C#教程

下一篇:C#四舍五入用法實例

本文標題:C#如何利用結(jié)構(gòu)體對固定格式數(shù)據(jù)進行解析

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5265.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有