24-09-17, 01:51 AM
(آخر تعديل لهذه المشاركة : 26-09-17, 03:53 PM {2} بواسطة silverlight.)
الدوال التالية مفيدة في التحويل من Structure الي مصفوفة بايت و العكس
تحويل Structure الي مصفوفة بايت
تحويل مصفوفة بايت الي Structure
مثال بسيط لكيفية استخدام الدوال
كتابة الدوال أعلاه علي هيئة كلاس
إستخدام الكلاس
أتمني أن يكون الكود مفيدا للبعض منكم
تقبلوا تحياتي
تحويل Structure الي مصفوفة بايت
PHP كود :
Private Function ToBytes(Of T As Structure)(value As T) As Byte()
Dim length As Integer = System.Runtime.InteropServices.Marshal.SizeOf(value)
Dim bytes As Byte() = New Byte(length - 1) {}
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
System.Runtime.InteropServices.Marshal.StructureToPtr(value, gc.AddrOfPinnedObject(), True)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return bytes
End Function
تحويل مصفوفة بايت الي Structure
PHP كود :
Private Function ToStructure(Of T As Structure)(bytes As Byte()) As T
Dim result As T = Nothing
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
result = CType((CObj(System.Runtime.InteropServices.Marshal.PtrToStructure(gc.AddrOfPinnedObject(), GetType(T)))), T)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return result
End Function
مثال بسيط لكيفية استخدام الدوال
PHP كود :
Dim bytes As Byte() = ToBytes(CLng(1000))
كتابة الدوال أعلاه علي هيئة كلاس
PHP كود :
Public Interface IStructureConverter(Of T As Structure)
Function ToBytes() As Byte()
Function ToStructure() As T
End Interface
Public Class StructurConverter(Of T As Structure)
Implements IStructureConverter(Of T)
Private _value As T
Private _bytes As Byte()
Public Sub New(value As T)
Me._value = value
End Sub
Public Sub New(bytes As Byte())
Me._bytes = bytes
End Sub
Public Function ToBytes() As Byte() Implements IStructureConverter(Of T).ToBytes
Return Me.ToBytes(Me._value)
End Function
Public Function ToStructure() As T Implements IStructureConverter(Of T).ToStructure
Return Me.ToStructure(Me._bytes)
End Function
Private Function ToBytes(value As T) As Byte()
Dim length As Integer = System.Runtime.InteropServices.Marshal.SizeOf(value)
Dim bytes As Byte() = New Byte(length - 1) {}
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
System.Runtime.InteropServices.Marshal.StructureToPtr(value, gc.AddrOfPinnedObject(), True)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return bytes
End Function
Private Function ToStructure(bytes As Byte()) As T
Dim result As T = Nothing
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
result = CType((CObj(System.Runtime.InteropServices.Marshal.PtrToStructure(gc.AddrOfPinnedObject(), GetType(T)))), T)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return result
End Function
End Class
إستخدام الكلاس
PHP كود :
Dim converter As IStructureConverter(Of Integer) = New StructurConverter(Of Integer)(12345)
Dim bytes As Byte() = converter.ToBytes
Me.Text = New StructurConverter(Of Integer)(bytes).ToStructure
أتمني أن يكون الكود مفيدا للبعض منكم
تقبلوا تحياتي
Retired