vb.net conversion byte array structure

  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 statement to a dummy ( 103 )
 68  
69              ReDim statement to BDate ( 8 )
 70  
71              ReDim statement to BTime ( 8 )
 72  
73              ReDim statement to BAID ( 8 )
 74  
75              ReDim statement to BEID ( 8 )
 76  
77              ReDim statement to BWeight ( 8 )
 78  
79              ReDim statement to BSexAge ( 8 )
 80  
81              ReDim statement to BComment ( 32 )
 82  
83              ReDim statement to BIMPID (16 )
 84  
85  
86          End Sub 
87  
88  
89  
90      End Structure 
91 is     
92  
93      Public g_SH of As sSftDataHeader
 94      Public m_fn of As the FileStream
 95      Public m_fr of As the BinaryReader
 96      Public m_fw of As the BinaryWriter
 97  
98  
99  
100      '' '  <Summary> 
101      ' '' Structure -converted into a byte array
 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          the Return TBYTE
 112      End Function 
113  
114      '' '  <Summary> 
115      ' '' convert a byte array structure
 116      '' '  </ Summary> 
117      '' '  <param name = "arrByte"> to be transformed byte array </ param> 
1 18      ' ''  <param name = "oType"> to be converted to the structure type </ 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

 

Guess you like

Origin www.cnblogs.com/Juli/p/11653144.html