• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

How can I convert a structure to a Byte string.

I have a structure that looks like:
  Record structure
    dim echo as integer
    dim type as Byte
    dim data as string
  end structure

    I am tring to send this structure in a UDP Socket but it has to be BYTE.  How do I convert the integer, keep the byte and convert the string to byte into to a byte array so that the server program gets the data in the correct format.
0
larryH
Asked:
larryH
  • 4
  • 4
1 Solution
 
Bob LearnedCommented:
Example:

Private Structure Test
  Dim Var1 As Short
  Dim Var2 As Short
End Structure

‘Start here
Dim Tst As Test ' Stuctuer variable
Dim ByteArray() As Byte
' initialize Structure (Dummmy  Values)
Tst.Var1 = 911
Tst.Var2 = 7
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Tst))
ReDim ByteArray(Marshal.SizeOf(Tst) - 1)
'now copy strcutre to Ptr pointer
Marshal.StructureToPtr(Tst, Ptr, False)
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(Tst))
‘now use ByteArray ready for use
Marshal.FreeHGlobal(Ptr)

Source:
Marshaling Structures
http://www.codeproject.com/vb/net/Marshaling_Structures.asp

Bob
0
 
larryHAuthor Commented:
The following is the code that I have come up with so far:

   Structure Sent
        Dim numb As Integer
        Dim type As Byte
        Dim skn() As Byte
    End Structure

    Private Sub cmdGetPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetPrice.Click

        Dim Rec As Sent ' Stuctuer variable
        Dim ByteArray() As Byte

        Rec.numb = 911
        Rec.type = 0
        Rec.skn = System.Text.Encoding.ASCII.GetBytes("400151704121")
        Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Rec))
        ReDim ByteArray(Marshal.SizeOf(Rec) - 1)

        'now copy strcutre to Ptr pointer
        Marshal.StructureToPtr(Rec, Ptr, False)
        Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(Rec))
        'now use ByteArray ready for use
        Marshal.FreeHGlobal(Ptr)

        Console.WriteLine("Sending data...")
        Dim Socket As New System.Net.Sockets.Socket( _
            System.Net.Sockets.AddressFamily.InterNetwork, _
            System.Net.Sockets.SocketType.Dgram, _
            System.Net.Sockets.ProtocolType.Udp)
        Dim IPE As New System.Net.IPEndPoint( _
            System.Net.IPAddress.Parse("10.0.8.20"), 6122)

        Socket.Connect(IPE)
        Socket.Send(ByteArray)
        Socket.Shutdown(Net.Sockets.SocketShutdown.Send)
        Dim a As String
        Console.WriteLine("Receiving data")
        Dim R As Int32
        Dim RecBuf(1024) As Byte
        R = Socket.Receive(RecBuf)
        Console.Write(System.Text.Encoding.ASCII.GetString(RecBuf, 5, R - 5))
        Console.ReadLine()
    End Sub

I still have some questions:
1) The value of " Marshal.SizeOf(Rec)" is returning a 12 instead of 18.
2) It also seems that the data being sent to the server is missing the skn number that is "400151704121".  The server returns that the skn has the wrong size but i think it is just missing. The type field contains a binary zero and I don't know if that is stopping the move.
0
 
Bob LearnedCommented:
Try this:

  Private Structure Sent
    Dim numb As Integer
    Dim type As Byte
    Dim skn() As Byte

    Public Shared ReadOnly Property SizeOf()
      Get
        Return Marshal.SizeOf(GetType(Integer)) + Marshal.SizeOf(GetType(Byte)) + 12
      End Get
    End Property
  End Structure

Why R - 5 in this line?
   System.Text.Encoding.ASCII.GetString(RecBuf, 5, R - 5)

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
larryHAuthor Commented:
This is how my code looks now:

Structure Sent
        Dim numb As Integer
        Dim type As Byte
        Dim skn() As Byte

        Public Shared ReadOnly Property SizeOf()
            Get
                Return Marshal.SizeOf(GetType(Integer)) + Marshal.SizeOf(GetType(Byte)) + 12
            End Get
        End Property

    End Structure

    Private Sub cmdGetPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetPrice.Click
        Dim i As Integer
        Dim Rec As Sent ' Stuctuer variable
        Dim ByteArray() As Byte
        Dim a As String
        Rec.numb = 911
        Rec.type = 0
        Rec.skn = System.Text.Encoding.ASCII.GetBytes("400151704121")
        Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Rec))
        ReDim ByteArray(Marshal.SizeOf(Rec) - 1)

        'now copy strcutre to Ptr pointer
        Marshal.StructureToPtr(Rec, Ptr, False)
        Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(Rec))
        'now use ByteArray ready for use
        Marshal.FreeHGlobal(Ptr)

        a = ByteArray(5)
        a = ByteArray(6)

        Console.WriteLine("Sending data...")
        Dim Socket As New System.Net.Sockets.Socket( _
            System.Net.Sockets.AddressFamily.InterNetwork, _
            System.Net.Sockets.SocketType.Dgram, _
            System.Net.Sockets.ProtocolType.Udp)
        Dim IPE As New System.Net.IPEndPoint( _
            System.Net.IPAddress.Parse("10.0.8.20"), 6122)

        Socket.Connect(IPE)
        i = Socket.Send(ByteArray)
        Socket.Shutdown(Net.Sockets.SocketShutdown.Send)
        '       Dim a As String
        Console.WriteLine("Receiving data")
        Dim R As Int32
        Dim RecBuf(1024) As Byte
        R = Socket.Receive(RecBuf)
        Console.Write(System.Text.Encoding.ASCII.GetString(RecBuf, 0, R))
        Console.ReadLine()
    End Sub

End Class

I still have the same problem. When I run this the size of ByteArray is still 12. Also the data in ByteArray seems to be incorrect. The first five bytes are correct then the rest is binary zeros. The fifth byte should be a binary zero but the 6th should be 4(52)., the 7th 0(48) ...
The answer to you question about -5 is I was tring juist to look at the SKN number that start in column 5 so i subtract 5 from the length.

Thanks Larry

0
 
Bob LearnedCommented:
ReDim ByteArray(Sent.SizeOf - 1)

Bob
0
 
larryHAuthor Commented:
That fixed the size but the data is still binary zeros after the fifth byte
The fifth byte should be a binary zero but the 6th should be 4(52)., the 7th 0(48) ...
0
 
Bob LearnedCommented:
I don't think that I know the answer to that one.  It might be because of the way strings are stored in memory.

Bob
0
 
larryHAuthor Commented:
Thanks for all the help - finally got my code to work.  It's ugly but works
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now