Xojo.Core.MemoryBlock
From Xojo Documentation
A read-only class for managing blocks of memory. Since the contents of a MemoryBlock cannot be altered, if you need to alter a MemoryBlock, use MutableMemoryBlock.
Properties | |||
|
Operators | |
|
Constructors | |||||
|
Notes
For 32-bit apps, you can request a maximum of about 2 to 3 GB depending on the OS. Generally Windows is closer to 2GB and macOS/Linux are closer to 3GB. There is no practical limit for 64-bit apps.
To convert Text to a MemoryBlock, use TextEncoding.ConvertTextToData. To convert a MemoryBlock to Text, use TextEncoding.ConvertDataToText.
Compared to MutableMemoryBlock, MemoryBlock is more efficient in terms of memory use savings, but doesn't really have any difference in terms of access. Being able to pass around immutable chunks of data allows the framework to directly reference internal data and avoid having to copy it on the off chance it might get modified.
DecodeHex
To convert a hex Text to a MemoryBlock you can use code like this:
Var bytes() As UInt8
For i As Integer = 0 To hex.Length - 2 Step 2
Var value As UInt8 = UInt8.FromHex(hex.Mid(i, 2))
bytes.AddRow(value)
Next
Var mb As New Xojo.Core.MemoryBlock(bytes)
EncodeHex
To EncodeHex the contents of a MemoryBlock you can use code like this:
For i As Integer = 0 To hashMB.Size - 1
Var b As UInt8 = hashMB.UInt8Value(i)
joiner.AddRow(b.ToHex(2))
Next
Var hex As Text = Text.Join(joiner, "")
Converting to a Classic MemoryBlock
When using both the Classic and New Framework in a single (desktop, web or console) app, you may find that you need to convert a Xojo.Core.MemoryBlock to a Classic MemoryBlock for use with other methods. You can do so by copying the data from the Xojo.Core.MemoryBlock to the Classic MemoryBlock:
Var temp As MemoryBlock = newMb.Data
Var mb As New MemoryBlock(newMb.Size)
mb.StringValue(0, mb.Size) = temp.StringValue(0, mb.Size)
temp = Nil // optional as it will also be cleared when it goes out of scope
See Also
Xojo.Core.MutableMemoryBlock, MemoryBlock classes