Xojo.Core.MemoryBlock

From Xojo Documentation

Class (inherits from Object)

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
Data fa-lock-32.png LittleEndian Size fa-lock-32.png
Methods
BooleanValue Int32Value SingleValue
CStringValue Int64Value UInt16Value
Clone Int8Value UInt32Value
CurrencyValue Left UInt64Value
DoubleValue Mid UInt8Value
IndexOf PtrValue WStringValue
Int16Value Right
Operators
Comparison
Constructors

Xojo.Core.MemoryBlock.Constructor(bytes() As Byte)


Xojo.Core.MemoryBlock.Constructor(other As MemoryBlock)


Xojo.Core.MemoryBlock.Constructor(p As Ptr)


Xojo.Core.MemoryBlock.Constructor(p As Ptr, size As UInteger)


Xojo.Core.MemoryBlock.Constructor(size As UInteger)


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:

// hex contains hex as Text
Dim bytes() As UInt8
For i As Integer = 0 To hex.Length - 2 Step 2
Dim value As UInt8 = UInt8.FromHex(hex.Mid(i, 2))
bytes.Append(value)
Next
Dim mb As New Xojo.Core.MemoryBlock(bytes)

EncodeHex

To EncodeHex the contents of a MemoryBlock you can use code like this:

Dim joiner() As Text
For i As Integer = 0 To hashMB.Size - 1
Dim b As UInt8 = hashMB.UInt8Value(i)
joiner.Append(b.ToHex(2))
Next
Dim 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:

// newMB is a Xojo.Core.MemoryBlock
Dim temp As MemoryBlock = newMb.Data
Dim 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