-
May 25th, 2012, 12:53 AM
#1
Thread Starter
Junior Member
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.
-
May 25th, 2012, 12:59 AM
#2
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
-
May 25th, 2012, 01:06 AM
#3
Re: Converting struct object to byte array
alternatively, you can use the Marshal class:
vb Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure s
Dim a As Integer
Dim b As String
Dim c As Boolean
End Structure
Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
'now copy structure to Ptr pointer
Marshal.StructureToPtr(myStruct, Ptr, False)
'copy to byte array
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
Marshal.FreeHGlobal(Ptr)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 25th, 2012, 01:20 AM
#4
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.
-
May 25th, 2012, 01:20 AM
#5
Thread Starter
Junior Member
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?
-
May 25th, 2012, 01:30 AM
#6
Re: Converting struct object to byte array
Originally Posted by jmcilhinney
.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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 25th, 2012, 01:33 AM
#7
Re: Converting struct object to byte array
Originally Posted by mk48
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:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure s
Dim a As Integer
Dim b As String
Dim c As Boolean
Public Overloads Shared Operator =(ByVal s1 As s, ByVal s2 As s) As Boolean
Return s1.a = s2.a AndAlso s1.b = s2.b AndAlso s1.c = s2.c
End Operator
Public Overloads Shared Operator <>(ByVal s1 As s, ByVal s2 As s) As Boolean
Return s1.a <> s2.a OrElse s1.b <> s2.b OrElse s1.c <> s2.c
End Operator
End Structure
Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
'now copy structure to Ptr pointer
Marshal.StructureToPtr(myStruct, Ptr, False)
'copy to byte array
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
Dim myStruct2 As New s
myStruct2 = DirectCast(Marshal.PtrToStructure(Ptr, GetType(s)), s)
MsgBox(myStruct = myStruct2)
Marshal.FreeHGlobal(Ptr)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 25th, 2012, 01:46 AM
#8
Re: Converting struct object to byte array
that last post should be:
vb Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure s
Dim a As Integer
Dim b As String
Dim c As Boolean
Public Overloads Shared Operator =(ByVal s1 As s, ByVal s2 As s) As Boolean
Return s1.a = s2.a AndAlso s1.b = s2.b AndAlso s1.c = s2.c
End Operator
Public Overloads Shared Operator <>(ByVal s1 As s, ByVal s2 As s) As Boolean
Return s1.a <> s2.a OrElse s1.b <> s2.b OrElse s1.c <> s2.c
End Operator
End Structure
Dim myStruct As New s With {.a = 1, .b = "one", .c = True}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))
Dim ByteArray(Marshal.SizeOf(myStruct) - 1) As Byte
'now copy structure to Ptr pointer
Marshal.StructureToPtr(myStruct, Ptr, False)
'copy to byte array
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(myStruct))
Dim Ptr2 As IntPtr = Marshal.AllocHGlobal(ByteArray.Length)
Marshal.Copy(ByteArray, 0, Ptr2, ByteArray.Length)
Dim myStruct2 As New s
myStruct2 = DirectCast(Marshal.PtrToStructure(Ptr2, GetType(s)), s)
MsgBox(myStruct = myStruct2)
Marshal.FreeHGlobal(Ptr)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 25th, 2012, 02:13 AM
#9
Thread Starter
Junior Member
Re: Converting struct object to byte array
Thanks! I knew something was off with the previous post considering you were using the same Ptr.
-
May 25th, 2012, 02:30 AM
#10
Thread Starter
Junior Member
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
-
May 25th, 2012, 02:39 AM
#11
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|