VB.NET structure BYTE array mutual conversion

 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 8)>
10 Dim bDate() As Byte
11 8)>
12 Dim bTime() As Byte
13 8)>
14 Dim bAID() As Byte
15 8)>
16 Dim bEID() As Byte
17 8)>
18 Dim bWeight() As Byte
19 8)>
20 Dim bSexAge() As Byte
21 32)>
22 Dim bComment() As Byte
23 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 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 '''
101 ''' The structure is converted to a byte array
102 '''

103 ''' The structure to be transformed
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 '''
115 ''' Convert byte array to structure
116 '''

117 ''' The byte array to be converted param>
118 ''' The type of structure to be converted< /param>
119 Public Function ByteArrayToStructure(ByVal arrByte() As Byte, ByVal oType As Type) < span style="color: #0000ff;">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

 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 8)>
10 Dim bDate() As Byte
11 8)>
12 Dim bTime() As Byte
13 8)>
14 Dim bAID() As Byte
15 8)>
16 Dim bEID() As Byte
17 8)>
18 Dim bWeight() As Byte
19 8)>
20 Dim bSexAge() As Byte
21 32)>
22 Dim bComment() As Byte
23 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 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 ‘‘‘
101 ‘‘‘ 结构体转换成字节数组
102 ‘‘‘

103 ‘‘‘ 要转化的结构体
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 ‘‘‘
115 ‘‘‘ 字节数组转换成结构体
116 ‘‘‘

117 ‘‘‘ 要转化的字节数组
118 ‘‘‘ 要转化成的结构体类型
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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3407 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.