Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually that is not "bad" VB6 code. What he is doing is storing the array in a compact binary format and saving it as a "chunk" into the database. There are lots of valid reasons to do this. </p> <p>The reason for the VB6 code saving it to disk and reading it back is because VB6 doesn't have native support for reading and writing files in memory only. This is the common algorithm if you want to create a chunk of binary data and stuff it somewhere else like a database field.</p> <p>It is not an issues dealing with this in .NET. The code I have is in VB.NET so you will have to convert it to C#.</p> <p>Modified to handle bytes and the unicode problem. </p> <pre><code>Public Function DataArrayFromDatabase(ByVal dbData As byte()) As Single(,) Dim bData(Ubound(dbData)/2) As Byte Dim I As Long Dim J As Long J=0 For I = 1 To Ubound(dbData) step 2 bData(J) = dbData(I) J=1 Next I Dim sM As New IO.MemoryStream(bData) Dim bR As IO.BinaryReader = New IO.BinaryReader(sM) Dim Dim1 As Integer = bR.ReadInt32 Dim Dim2 As Integer = bR.ReadInt32 Dim newData(Dim1, Dim2) As Single For I = 0 To Dim2 For J = 0 To Dim1 newData(J, I) = bR.ReadSingle Next Next bR.Close() sM.Close() Return newData End Function </code></pre> <p>The key trick is to read in the data just like if you were in VB6. We have the ability to use MemoryStreams in .NET so this is fairly easy.</p> <p>First we skip every other byte to eliminate the Unicode padding. </p> <p>Then we create a memorystream from the array of bytes. Then a BinaryReader initialized with the MemoryStream.</p> <p>We read in the first dimension of the array a VB6 Long or .NET Int32 We read in the second dimension of the array a VB6 Long or .NET Int32</p> <p>The read loops are constructed in reverse order of the array's dimension. Dim2 is the outer loop and Dim1 is the inner. The reason for this is that this is how VB6 store arrays in binary format. </p> <p>Return newData and you have successfully restored the original array that was created in VB6!</p> <p>Now you could try to use some math trick. The two dimension are 4 bytes/characters and each array element is 4 bytes/characters. But for long term maintainability I find using byte manipulation with memorystreams a lot more explicit. It take a little more code but a lot more clear when you revisit it 5 years from now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload