تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
Convert structure to byteArray & viceversa
#1
الدوال التالية مفيدة في التحويل من 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(bytesSystem.Runtime.InteropServices.GCHandleType.Pinned)
 
           System.Runtime.InteropServices.Marshal.StructureToPtr(valuegc.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 Nothing
        Dim gc 
As System.Runtime.InteropServices.GCHandle Nothing
        Try
            gc 
System.Runtime.InteropServices.GCHandle.Alloc(bytesSystem.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 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(bytesSystem.Runtime.InteropServices.GCHandleType.Pinned)
 
           System.Runtime.InteropServices.Marshal.StructureToPtr(valuegc.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 Nothing
        Dim gc 
As System.Runtime.InteropServices.GCHandle Nothing
        Try
            gc 
System.Runtime.InteropServices.GCHandle.Alloc(bytesSystem.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
الرد }}}
تم الشكر بواسطة: ابو ليلى , Fantastico



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم