Results 1 to 11 of 11

Thread: Converting struct object to byte array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Converting struct object to byte array

    Hi,

    I am needing to write data to a usb device. The USB device uses a byte array (DataArray() as byte) to store the data. How can I assign a struct object (myStruct(0)) to the byte array. I run into the error "Value of type byte cannot be converted to 1-dimensional array of byte" when I try to cast the struct as a byte.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Converting struct object to byte array

    You can use serialization to convert an object to binary data and vice versa. You would use a BinaryFormatter to write the object to a MemoryStream and then call GetBuffer to get the stream contents into a Byte array.

    https://www.google.com.au/search?q=b...ient=firefox-a

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Converting struct object to byte array

    alternatively, you can use the Marshal class:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure s
    6.         Dim a As Integer
    7.         Dim b As String
    8.         Dim c As Boolean
    9.     End Structure
    10.     Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
    11.  
    12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.         Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
    14.         Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
    15.         'now copy structure to Ptr pointer
    16.         Marshal.StructureToPtr(myStruct, Ptr, False)
    17.         'copy to byte array
    18.         Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
    19.         Marshal.FreeHGlobal(Ptr)
    20.  
    21.     End Sub
    22.  
    23. End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Converting struct object to byte array

    .paul., are you aware of any potential pitfalls with either approach? I have to admit that I've never really had cause to use either so I'm not sure if one option has specific benefit over the other. I think that the code would be about the same length.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting struct object to byte array

    Thanks guys. If I use the marshal method, how can I read back the value from the byte array as a struct to confirm that it was sent correctly?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Converting struct object to byte array

    Quote Originally Posted by jmcilhinney View Post
    .paul., are you aware of any potential pitfalls with either approach? I have to admit that I've never really had cause to use either so I'm not sure if one option has specific benefit over the other. I think that the code would be about the same length.
    not that i know of, but it does use unmanaged memory.
    not sure how reliable that is...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Converting struct object to byte array

    Quote Originally Posted by mk48 View Post
    Thanks guys. If I use the marshal method, how can I read back the value from the byte array as a struct to confirm that it was sent correctly?
    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure s
    6.         Dim a As Integer
    7.         Dim b As String
    8.         Dim c As Boolean
    9.         Public Overloads Shared Operator =(ByVal s1 As s, ByVal s2 As s) As Boolean
    10.             Return s1.a = s2.a AndAlso s1.b = s2.b AndAlso s1.c = s2.c
    11.         End Operator
    12.         Public Overloads Shared Operator <>(ByVal s1 As s, ByVal s2 As s) As Boolean
    13.             Return s1.a <> s2.a OrElse s1.b <> s2.b OrElse s1.c <> s2.c
    14.         End Operator
    15.     End Structure
    16.     Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
    17.  
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
    20.         Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
    21.         'now copy structure to Ptr pointer
    22.         Marshal.StructureToPtr(myStruct, Ptr, False)
    23.         'copy to byte array
    24.         Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
    25.  
    26.         Dim myStruct2 As New s
    27.         myStruct2 = DirectCast(Marshal.PtrToStructure(Ptr, GetType(s)), s)
    28.         MsgBox(myStruct = myStruct2)
    29.  
    30.         Marshal.FreeHGlobal(Ptr)
    31.     End Sub
    32.  
    33. End Class

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Converting struct object to byte array

    that last post should be:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure s
    6.         Dim a As Integer
    7.         Dim b As String
    8.         Dim c As Boolean
    9.         Public Overloads Shared Operator =(ByVal s1 As s, ByVal s2 As s) As Boolean
    10.             Return s1.a = s2.a AndAlso s1.b = s2.b AndAlso s1.c = s2.c
    11.         End Operator
    12.         Public Overloads Shared Operator <>(ByVal s1 As s, ByVal s2 As s) As Boolean
    13.             Return s1.a <> s2.a OrElse s1.b <> s2.b OrElse s1.c <> s2.c
    14.         End Operator
    15.     End Structure
    16.     Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
    17.  
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
    20.         Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
    21.         'now copy structure to Ptr pointer
    22.         Marshal.StructureToPtr(myStruct, Ptr, False)
    23.         'copy to byte array
    24.         Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
    25.  
    26.         Dim Ptr2 As IntPtr = Marshal.AllocHGlobal(ByteArray.Length)
    27.         Marshal.Copy(ByteArray, 0, Ptr2, ByteArray.Length)
    28.         Dim myStruct2 As New s
    29.         myStruct2 = DirectCast(Marshal.PtrToStructure(Ptr2, GetType(s)), s)
    30.  
    31.         MsgBox(myStruct = myStruct2)
    32.  
    33.         Marshal.FreeHGlobal(Ptr)
    34.     End Sub
    35.  
    36. End Class

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting struct object to byte array

    Thanks! I knew something was off with the previous post considering you were using the same Ptr.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting struct object to byte array

    When I try this code:

    Code:
    TestStruct = DirectCast(Marshal.PtrToStructure(Ptr2, GetType(TestStruct)), teststruct)
    I receive the error "Type TestStruct is not defined" although TestStruct is a structure object

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting struct object to byte array

    I fixed it. I specified the struct object name instead of the struct type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width