Coerce a byte string to a structure?

I have a file that contains a series of packets. I've created a set of structures that map the elements of the packets. My question...

Is there a technique in VB to map the string of bytes read from the file to the structures

In 'C' this would be simple. In VB.NET I don't know. Suggestions are appreciated.

Thanks, Johnny




Answer this question

Coerce a byte string to a structure?

  • Dipesh A.

    Johnny,
     there is something in the Interopservices that you may be able to use.
    I'm not sure what the overhead will be compared to just bitconverting the values out.
    Also, having arrays in your structure will be a problem / unknown length
    Anyways, this technique copies your managed bytes back to unmanaged memory and marshals them back into a managed structure. That goes something like this:

    Imports System.Runtime.InteropServices

    Dim lpBuffer As IntPtr
    Dim size as Integer

    ... 'size = Figure out how many bytes your data will occupy.

    lpBuffer = Marshal.AllocHGlobal(size) 'Allocate unmanaged memory for your data

    ... 'Now coax your data into this unmanaged buffer somehow (Hint: Marshal.Copy has overloads to do this)

    'Make a managed structure from unmanaged bytes.
    Marshal.PtrToStructure(lpBuffer,yourStructure)   

    A more detailed article about the general problem described in your question is here:
    http://dotnetjunkies.com/Article/DF6E5BE5-BCB6-43BB-BEE4-1013A2E3D085.dcik

    hth,
    Mac


  • Anton Rapoport

    Richard,

    Thanks. The big picture view is very helpful. I'll look into serialization.

    Johnny



  • Phonics3k

    Hi Johnny

    .NET is a managed world. AppDomains have a garbage collector that handles memory allocations, defragmentation etc so it is very difficult to work with memory directly.

    These days most people opt to use a binary serialization technique for this kind of situation. Type instances can be persisted permanently and rehydrated simply when needed.

    If this sounds like a viable alternative for you and you need some help, please let me know.

    If not, you do have the option of working with memory direct, but C# is perhaps a more suited language for this with its native abilities. There are some good examples of overcoming the immutable nature of some objects (eg strings) on the internet, but I guess such 'power' techniques are fragile in the sense that they may break in future framework editions.

    Hope the above is relevant and of use.

    Richard


  • Seth Livingston

     

    Hi,

    If you have an array in your STRUCTURES(s) you could use the string.split method.

    See.>> http://msdn2.microsoft.com/en-us/library/b873y76a(VS.80).aspx

    see also the StreamReader.Readline method.>>

    http://search.msdn.microsoft.com/search/default.aspx siteId=0&tab=0&query=readline

    Hope these help you....

     

    Regards,

    S_DS

     



  • Yeago

    Hi,

    See this code snippet with a STRUCTURE.>>

    Hope it helps.>

    What you could do is put an array of BYTE into one of your structures and use something like this.>>

    Trouble is you get the dreaded Object is not set to an instance of an object if you don't initialise the array elements first so i've commented out the line that puts BYTE values into a STRUCTURE's ARRAY.

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim byteArray(2) As Byte

    byteArray(0) = CByte("111")

    byteArray(1) = CByte("123")

    byteArray(2) = CByte("222")

    'This string is 3 byte values joined together 0,255,128

    Dim myString As String = "000255128"

    Dim index, arrayindex As Integer

    'Set a variable to my Structure named myBytes.

    Dim toStructure As mybytes

    For index = 0 To 2

    byteArray(index) = CByte(myString.Substring(index * 3, 3))

    'Put it in a Structure element. This next line gives the error as array elements.

    ''' are not pre-initialised.>>

    '''''toStructure.bite(index) = CByte(myString.Substring(index * 3, 3))

    Next

    'Note byteArray element number 2 is the 3rd item counting from nought.

    ' bytearray(2) value changes from 222 to 128 as demonstrated here.

    MsgBox("myBytes array value 2 is " & byteArray(2).ToString)

    End Sub

     

    Public Structure mybytes

    Dim bite() As Byte

    End Structure

     

     

    Regards,

    S_DS

     

     



  • vackol

    Thanks for the feedback S_DS.

    Here's some pseudocode of the sort of structure I'm dealing with...

    structure st_mainPacket
    dim sliceSize as unsigned int
    dim cntRecoveryFiles as unsigned int
    dim aIDRecovery[] as unsigned int 'array size is multiple of cntRecoveryFiles
    dim aIDNonRecovery[] as unsigned int
    end structure

    I can use bitconvert to retrieve the values, but the overhead is going to be heavy. In C I could dynamically allocate the arrays based on the value of cnt, then coerce the pointer to the arrays so I could reference them without any additional overhead.

    VB is an incredibly flexible tool, but I suspect I've run into a wall here.

    Thanks,

    Johnny



  • Coerce a byte string to a structure?