码迷,mamicode.com
首页 > 编程语言 > 详细

vb.net 结构体 byte数组相互转换

时间:2019-10-11 12:27:33      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:comm   read   date   binary   comment   iter   cti   hnu   man   

  1 Imports System.Runtime.InteropServices
  2 Imports System.IO
  3 
  4 Module MdSftData
  5 
  6     256 bytes
  7     Public Structure sSftDataHeader
  8         96 bytes
  9         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 10         Dim bDate() As Byte
 11         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 12         Dim bTime() As Byte
 13         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 14         Dim bAID() As Byte
 15         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 16         Dim bEID() As Byte
 17         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 18         Dim bWeight() As Byte
 19         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
 20         Dim bSexAge() As Byte
 21         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)>
 22         Dim bComment() As Byte
 23         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)>
 24         Dim bIMPID() As Byte
 25 
 26         8 bytes
 27         Dim nSamplingRate As Short
 28         Dim nChnums As Byte
 29         Dim nDataMode As Byte
 30         Dim type1 As Byte
 31         Dim type2 As Byte
 32         Dim type3 As Byte
 33         Dim type4 As Byte
 34 
 35         48 bytes
 36         Dim Cal1 As Short
 37         Dim Upper1 As Short
 38         Dim Lower1 As Short
 39         Dim Base1 As Short
 40         Dim wave_color_1 As UInteger
 41 
 42 
 43         Dim Cal2 As Short
 44         Dim Upper2 As Short
 45         Dim Lower2 As Short
 46         Dim Base2 As Short
 47         Dim wave_color_2 As UInteger
 48 
 49 
 50         Dim Cal3 As Short
 51         Dim Upper3 As Short
 52         Dim Lower3 As Short
 53         Dim Base3 As Short
 54         Dim wave_color_3 As UInteger
 55 
 56         Dim Cal4 As Short
 57         Dim Upper4 As Short
 58         Dim Lower4 As Short
 59         Dim Base4 As Short
 60         Dim wave_color_4 As UInteger
 61 
 62         104bytes
 63         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=104)>
 64         Dim dummy() As Byte
 65 
 66         Public Sub New(ByRef size As Integer)
 67             ReDim dummy(103)
 68 
 69             ReDim bDate(8)
 70 
 71             ReDim bTime(8)
 72 
 73             ReDim bAID(8)
 74 
 75             ReDim bEID(8)
 76 
 77             ReDim bWeight(8)
 78 
 79             ReDim bSexAge(8)
 80 
 81             ReDim bComment(32)
 82 
 83             ReDim bIMPID(16)
 84 
 85 
 86         End Sub
 87 
 88 
 89 
 90     End Structure
 91    
 92 
 93     Public g_SH As sSftDataHeader
 94     Public m_fn As FileStream
 95     Public m_fr As BinaryReader
 96     Public m_fw As BinaryWriter
 97 
 98 
 99 
100     ‘‘‘ <summary>
101     ‘‘‘ 结构体转换成字节数组
102     ‘‘‘ </summary>
103     ‘‘‘ <param name="oStructure">要转化的结构体</param>
104     Public Function StructureToByteArray(ByVal oStructure As Object) As Byte()
105         Dim oSize As Integer = Marshal.SizeOf(oStructure)
106         Dim tByte(oSize - 1) As Byte
107         Dim tPtr As IntPtr = Marshal.AllocHGlobal(oSize)
108         Marshal.StructureToPtr(oStructure, tPtr, False)
109         Marshal.Copy(tPtr, tByte, 0, oSize)
110         Marshal.FreeHGlobal(tPtr)
111         Return tByte
112     End Function
113 
114     ‘‘‘ <summary>
115     ‘‘‘ 字节数组转换成结构体
116     ‘‘‘ </summary>
117     ‘‘‘ <param name="arrByte">要转化的字节数组</param>
118     ‘‘‘ <param name="oType">要转化成的结构体类型</param>
119     Public Function ByteArrayToStructure(ByVal arrByte() As Byte, ByVal oType As Type) As Object
120         Dim oSize As Integer = Marshal.SizeOf(oType)
121         If oSize > arrByte.Length Then Return Nothing
122 
123         Dim tPtr As IntPtr = Marshal.AllocHGlobal(oSize)
124         Marshal.Copy(arrByte, 0, tPtr, oSize)
125         Dim oStructure As Object = Marshal.PtrToStructure(tPtr, oType)
126         Marshal.FreeHGlobal(tPtr)
127         Return oStructure
128     End Function
129 
130 
131 
132 
133 End Module

 

vb.net 结构体 byte数组相互转换

标签:comm   read   date   binary   comment   iter   cti   hnu   man   

原文地址:https://www.cnblogs.com/Juli/p/11653144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!