Difference between revisions of "Xojo.Core.MemoryBlock"

From Xojo Documentation

(See Also)
Line 58: Line 58:
 
<rbcode>
 
<rbcode>
 
// hex contains hex as Text
 
// hex contains hex as Text
Dim bytes() As UInt8
+
Var bytes() As UInt8
 
For i As Integer = 0 To hex.Length - 2 Step 2
 
For i As Integer = 0 To hex.Length - 2 Step 2
   Dim value As UInt8 = UInt8.FromHex(hex.Mid(i, 2))
+
   Var value As UInt8 = UInt8.FromHex(hex.Mid(i, 2))
   bytes.Append(value)
+
   bytes.AddRow(value)
 
Next
 
Next
Dim mb As New Xojo.Core.MemoryBlock(bytes)
+
Var mb As New Xojo.Core.MemoryBlock(bytes)
 
</rbcode>
 
</rbcode>
 
=== EncodeHex ===
 
=== EncodeHex ===
 
To EncodeHex the contents of a MemoryBlock you can use code like this:
 
To EncodeHex the contents of a MemoryBlock you can use code like this:
 
<rbcode>
 
<rbcode>
Dim joiner() As Text
+
Var joiner() As Text
 
For i As Integer = 0 To hashMB.Size - 1
 
For i As Integer = 0 To hashMB.Size - 1
   Dim b As UInt8 = hashMB.UInt8Value(i)
+
   Var b As UInt8 = hashMB.UInt8Value(i)
   joiner.Append(b.ToHex(2))
+
   joiner.AddRow(b.ToHex(2))
 
Next
 
Next
Dim hex As Text = Text.Join(joiner, "")
+
Var hex As Text = Text.Join(joiner, "")
 
</rbcode>
 
</rbcode>
  
Line 80: Line 80:
 
<rbcode>
 
<rbcode>
 
// newMB is a Xojo.Core.MemoryBlock
 
// newMB is a Xojo.Core.MemoryBlock
Dim temp As MemoryBlock = newMb.Data
+
Var temp As MemoryBlock = newMb.Data
Dim mb As New MemoryBlock(newMb.Size)
+
Var mb As New MemoryBlock(newMb.Size)
 
mb.StringValue(0, mb.Size) = temp.StringValue(0, mb.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
 
temp = Nil // optional as it will also be cleared when it goes out of scope

Revision as of 17:30, 11 August 2019

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

Constructor(bytes() As Byte)


Constructor(other As MemoryBlock)


Constructor(p As Ptr)


Constructor(p As Ptr, size As UInteger)


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
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:

Var joiner() As Text
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:

// newMB is a Xojo.Core.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