自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#結(jié)構(gòu)體數(shù)組間的轉(zhuǎn)化淺析

開發(fā) 后端
C#結(jié)構(gòu)體數(shù)組間的轉(zhuǎn)化問題:在寫C#TCP通信程序時(shí),發(fā)送數(shù)據(jù)時(shí),只能發(fā)送byte數(shù)組,處理起來比較麻煩,可以按本文介紹的方法實(shí)現(xiàn)。

C#結(jié)構(gòu)體數(shù)組間的轉(zhuǎn)化問題:在寫C#TCP通信程序時(shí),發(fā)送數(shù)據(jù)時(shí),只能發(fā)送byte數(shù)組,處理起來比較麻煩,可以按以下方法實(shí)現(xiàn):

(1)C#結(jié)構(gòu)體數(shù)組轉(zhuǎn)化之定義結(jié)構(gòu)體:

  1. //命名空間  
  2. using System.Runtime.InteropServices;  
  3.  
  4. //注意這個(gè)屬性不能少  
  5. [StructLayoutAttribute(  
  6. LayoutKind.Sequential,  
  7. CharSet=CharSet.Ansi,Pack=1)]  
  8. struct TestStruct  
  9. ...{  
  10. public int c;  
  11. //字符串,SizeConst為字符串的***長(zhǎng)度  
  12. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]  
  13. public string str;  
  14. //int數(shù)組,SizeConst表示數(shù)組的個(gè)數(shù),在轉(zhuǎn)換成  
  15. //byte數(shù)組前必須先初始化數(shù)組,再使用,初始化  
  16. //的數(shù)組長(zhǎng)度必須和SizeConst一致,例test = new int[6];  
  17. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]  
  18. public int[] test;  

(2)C#結(jié)構(gòu)體數(shù)組轉(zhuǎn)化之結(jié)構(gòu)體轉(zhuǎn)byte數(shù)組:

  1. /**//// <summary>  
  2. /// 結(jié)構(gòu)體轉(zhuǎn)byte數(shù)組  
  3. /// </summary>  
  4. /// <param name="structObj">要轉(zhuǎn)換的結(jié)構(gòu)體</param>  
  5. /// <returns>轉(zhuǎn)換后的byte數(shù)組</returns>  
  6. public static byte[] StructToBytes(object structObj)  
  7. ...{  
  8. //得到結(jié)構(gòu)體的大小  
  9. int size = Marshal.SizeOf(structObj);  
  10. //創(chuàng)建byte數(shù)組  
  11. byte[] bytes = new byte[size];  
  12. //分配結(jié)構(gòu)體大小的內(nèi)存空間  
  13. IntPtr structPtr = Marshal.AllocHGlobal(size);  
  14. //將結(jié)構(gòu)體拷到分配好的內(nèi)存空間  
  15. Marshal.StructureToPtr(structObj, structPtr, false);  
  16. //從內(nèi)存空間拷到byte數(shù)組  
  17. Marshal.Copy(structPtr, bytes, 0, size);  
  18. //釋放內(nèi)存空間  
  19. Marshal.FreeHGlobal(structPtr);  
  20. //返回byte數(shù)組  
  21. return bytes;  

C#結(jié)構(gòu)體數(shù)組轉(zhuǎn)化的問題就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#結(jié)構(gòu)體數(shù)組轉(zhuǎn)化有所幫助。

【編輯推薦】

  1. C#讀取配置文件詳解
  2. C#讀取Excel的簡(jiǎn)單實(shí)現(xiàn)
  3. C#讀取Excel數(shù)據(jù)簡(jiǎn)析
  4. C#讀取Excel遇到無法讀取的解決方法
  5. C#結(jié)構(gòu)體的特點(diǎn)淺析
責(zé)任編輯:仲衡 來源: 搜狐博客
相關(guān)推薦

2009-08-13 13:17:10

C#結(jié)構(gòu)體數(shù)組

2009-08-13 13:29:04

C#結(jié)構(gòu)體使用

2009-08-13 11:18:50

C#結(jié)構(gòu)體

2009-08-13 14:56:46

C#的結(jié)構(gòu)體使用

2009-08-27 16:18:47

C#類C#結(jié)構(gòu)體

2009-08-31 15:02:22

C#解析結(jié)構(gòu)體指針

2009-08-13 14:06:37

C#結(jié)構(gòu)體結(jié)構(gòu)體和類的區(qū)別

2009-09-23 09:36:34

C#數(shù)組

2009-08-26 13:07:07

C#交錯(cuò)數(shù)組

2009-09-02 10:58:02

C#動(dòng)態(tài)數(shù)組

2009-08-14 11:05:28

C#語言的結(jié)構(gòu)體

2009-08-13 14:46:03

C#結(jié)構(gòu)體定義

2009-08-06 10:14:15

C#引用類型數(shù)組

2009-08-07 13:39:13

C#定義整型數(shù)組

2009-08-07 11:26:53

C#數(shù)組結(jié)構(gòu)

2009-08-13 14:24:44

C#結(jié)構(gòu)體構(gòu)造函數(shù)

2009-08-28 14:25:57

C# byte數(shù)組

2009-09-18 10:58:31

C#數(shù)組操作

2009-08-13 15:03:58

C#結(jié)構(gòu)體變量

2010-12-30 09:22:58

C語言 數(shù)組
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)