Difference between revisions of "Xojo.Core.MemoryBlock"
From Xojo Documentation
|Line 1:||Line 1:|
Latest revision as of 21:54, 13 November 2020
| This item was deprecated in version 2020r2.
Please use MemoryBlock as a replacement.
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.
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.
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))
Var mb As New Xojo.Core.MemoryBlock(bytes)
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)
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