# Array of Double to MemoryBlock conversion

Is there an easy way to get an array of Double to become a MemoryBlock containing the consecutive double values?

You can use this function:

[code]
Function DoubleArrToMB(arr() As Double) As MemoryBlock
Dim mb As MemoryBlock
Dim i As Integer

mb = new MemoryBlock((UBound(arr) + 1) * 8)
for i = 0 to UBound(arr)
mb.DoubleValue(i * 8) = arr(i)
next i

return mb
End Function[/code]

Not sure if there is a built-in way to convert an array to an MB. Would be a great feature though.

If you’re going to do that, I’d suggest writing the number of elements first. Otherwise you won’t know how many to read back later if your memoryblock contains other data.

Good suggestion. Here is the revised method that now stores the element count:

[code]Function DoubleArrToMB(arr() As Double) As MemoryBlock
Dim mb As MemoryBlock
Dim i As Integer

mb = new MemoryBlock((UBound(arr) + 2) * 8)

mb.Int64Value(0) = UBound(arr)

for i = 0 to UBound(arr)
mb.DoubleValue((i + 1) * 8) = arr(i)
next i

return mb
End Function
[/code]

And its inverted cousin…

[code]Function MBToDoubleArr(mb As MemoryBlock) As Double()
Dim ub As Integer
Dim i As Integer
Dim arr() As Double

ub = mb.Int64Value(0)

for i = 0 to ub
arr.Append mb.DoubleValue((i + 1) * 8)
next i

return arr
End Function
[/code]