Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

Previous items

MemoryBlock.MidMBS(offset as Integer) as memoryblock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 3.3 Yes Yes Yes Yes No
Function: Returns a memoryblock with the bytes of the given memoryblock from the given offset on.
Notes:
Offset is 0 based.
Returns nil if offset<0 or on low memory.
This function will not work if the memoryblock has an unknown size.

See also:

Some examples which use this method:

Feedback, Comments & Corrections

MemoryBlock.MidMBS(offset as Integer, length as Integer) as memoryblock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 3.3 Yes Yes Yes Yes No
Function: Returns a memoryblock with the given bytes from the middle of the memoryblock.
Notes:
Offset is 0 based.
Returns nil if offset<0 or on low memory.
If length is too long then length is set to a lower value.
This function will not work if the memoryblock has an unknown size.

See also:

Some examples which use this method:

Feedback, Comments & Corrections

MemoryBlock.MinMBS(firstMem as Ptr, secondMem as Ptr, BitSize as Integer = 8, Signed as Boolean = false, offsetByte as Integer = 0, lengthBytes as Integer = 0) as boolean
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 14.2 Yes Yes Yes Yes No
Function: Calculates min values of values in memory.
Example:
dim m1 as new MemoryBlock(24)
dim m2 as new MemoryBlock(24)
dim mr1 as new MemoryBlock(24)
dim mr2 as new MemoryBlock(24)

for i as Integer = 0 to 11
m1.UInt16Value(2*i) = i
m2.UInt16Value(2*(11-i)) = i
next

if mr1.minMBS(m1, m2, 16, false, 0, 24) then
if mr2.maxMBS(m1, m2, 16, false, 0, 24) then

MsgBox EncodeHex(m1)+" first"+EndOfLine+_
EncodeHex(m2)+" second"+EndOfLine+_
EncodeHex(mr1)+" min"+EndOfLine+_
EncodeHex(mr2)+" max"

end if
end if
Notes:
firstMem and secondMem can be ptr or memoryblock with some values. Can be same as destination (the memoryblock the method is called at)
BitSize defines integer bit depth 8, 16, 32 or 64. Signed defines if to expect signed or unsigned integers.
offsetByte defines offset in destination memoryblock. lengthBytes is length of memory. If zero, we use size of destination memoryblock.
Passing bad parameters can easily lead to crash. Return true on success and false on failure and raises exceptions for invalid parameters.

Some examples which use this method:

Feedback, Comments & Corrections

MemoryBlock.MirrorBitsInBytesMBS(offsetByte as Integer, lengthByte as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 11.1 Yes Yes Yes Yes No
Function: Mirrors bits in each byte.
Example:
dim m as new MemoryBlock(12)

m.CString(0)="HelloHello"

m.MirrorBitsInBytesMBS(0,5)

MsgBox EncodingToHexMBS(m.StringValue(5,5))+" -> "+EncodingToHexMBS(m.StringValue(0,5))
Notes:
offsetByte: where to start in the memoryblock
lengthByte: number of bytes to swap

Mirror means in this case, that if you a byte with bits 11001100, after the mirror, you have 00110011. So bit 0 and 7 exchange values. Same for 1 and 6, 2 and 5, 3 and 4.

Feedback, Comments & Corrections

MemoryBlock.MirrorBitsMBS(offsetBit as Integer, lengthBit as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 7.1 Yes Yes Yes Yes No
Function: Mirrors the bits in a memoryblock.
Notes:
e.g. "111100001" in the memoryblock would give "100001111"
This function is certainly not the fastest one, but faster than anything you can get written in Realbasic.
And make sure the bounds are matched, because on memory access outside the valid ranges, the function will crash.
0 ≤ offsetBit < mem.size*8 and 0 ≤ lengthBit ≤ mem.size*8 - offsetBit

Feedback, Comments & Corrections

MemoryBlock.MirrorBytesMBS(offsetByte as Integer = 0, lengthByte as Integer = -1)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 7.1 Yes Yes Yes Yes No
Function: Mirrors the bytes in the given range.
Notes:
e.g. "abcdefgh" in the memoryblock would give "hgfedcba"
This function is certainly not the fastest one, but faster than anything you can get written in Realbasic.
And make sure the bounds are matched, because on memory access outside the valid ranges, the function will crash.
0 ≤ offsetByte < mem.size and 0 ≤ lengthByte ≤ mem.size - offsetByte

If lengthByte is -1, we query memoryblock for size. If size is unknown or negative, the function does nothing.

Feedback, Comments & Corrections

MemoryBlock.MultiplyUInt16MBS(Factor as Double, offsetByte as Integer = 0, lengthBytes as Integer = 0, MultiplyOnlyOver as Integer = -1)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 14.1 Yes Yes Yes Yes No
Function: Multiplied UInt16 values in the memoryblock.
Example:
dim myMemOut3 as new MemoryBlock(500)
myMemOut3.MultiplyUInt16MBS(0.9)
Notes:
Values bigger than 65535 are set to 65535.
lengthBytes is in bytes, so 2 times the number of values.
Factor must be >= 0.0.
Offset is the offset in bytes from the beginning of the memoryblock to start. Wrong offsets (e.g. negative) can lead to crashes. lengthBytes is optional, if not specified or zero uses the length of memoryblock.

Added MultiplyOnlyOver parameter in version 18.4. If negative, we multiply all values. If positive, we multiply only values > the given value.

Feedback, Comments & Corrections

MemoryBlock.MultiplyUInt8MBS(Factor as Double, offsetByte as Integer = 0, lengthBytes as Integer = 0, MultiplyOnlyOver as Integer = -1)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 14.1 Yes Yes Yes Yes No
Function: Multiplies UInt8 values in the memoryblock.
Example:
dim myMemOut3 as new MemoryBlock(500)
myMemOut3.MultiplyUInt8MBS(0.9)
Notes:
Values bigger than 255 are set to 255.
lengthBytes is in bytes, so 2 times the number of values.
Factor must be >= 0.0.
Offset is the offset in bytes from the beginning of the memoryblock to start. Wrong offsets (e.g. negative) can lead to crashes. lengthBytes is optional, if not specified or zero uses the length of memoryblock.

Added MultiplyOnlyOver parameter in version 18.4. If negative, we multiply all values. If positive, we multiply only values > the given value.

Feedback, Comments & Corrections

NewMemoryBlockFromPtrMBS(ptr as Integer) as memoryblock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
global method MemoryBlock MBS Util Plugin Yes Yes Yes Yes No
Function: Creates a memoryblock for a given address in memory.

Some examples using this method:

Feedback, Comments & Corrections

MemoryBlock.OSTypeMBS(offset as Integer) as String
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
property MemoryBlock MBS Util Plugin Yes Yes Yes Yes No
Function: Read/Write an OSType.
Notes:
Interprets 4 bytes starting at the given offset as a OSType value.
(Read and Write computed property)

Feedback, Comments & Corrections

MemoryBlock.RightMBS(length as Integer) as memoryblock
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 3.3 Yes Yes Yes Yes No
Function: Returns a memoryblock with the given number of bytes from the right side of the memoryblock.
Example:
dim mem,m as MemoryBlock
mem=m.RightMBS(5)
Notes:
If length is more than the memoryblock's size, than length is set to size.
Returns nil if length<=0 or on low memory.
This function will not work if the memoryblock has an unknown size.

Some examples which use this method:

Feedback, Comments & Corrections

MemoryBlock.SetStringMBS(str as String, offset as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin Yes Yes Yes Yes No
Function: Sets a string inside the memoryblock.
Notes: With newer RB versions you may better use StringValue.

Feedback, Comments & Corrections

MemoryBlock.SwapBytes16MBS(offset as Integer, numBytes as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 4.0 Yes Yes Yes Yes No
Function: Swaps words inside the given bounds inside the memoryblock.
Example:
dim numbytes as Integer // number of bytes in MemoryBlock
dim m as MemoryBlock // your memoryblock
dim i as Integer

for i=0 to numbytes step 2
m.UShort(i)=EndianSwap16MBS(m.UShort(i))
next

Feedback, Comments & Corrections

MemoryBlock.SwapBytes32MBS(offset as Integer, numBytes as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin 4.0 Yes Yes Yes Yes No
Function: Swaps longs inside the given bounds inside the memoryblock.
Example:
// Test SwapBytes32MBS and SwapBytes16MBS:

const h11223344=&h11223344
const h22114411=&h22114433 // 16bit swap
const h44332211=&h44332211 // 32bit swap

dim m as memoryBlock
dim ok as Integer

m=NewmemoryBlock(20)
m.Long(00)=h11223344
m.Long(04)=h11223344
m.Long(08)=h11223344
m.Long(12)=h11223344
m.Long(16)=h11223344

m.SwapBytes16MBS(4,4)
m.SwapBytes32MBS(12,4)

if m.Long(00)=h11223344 then
ok=ok+1
else
MsgBox "00: "+hex(m.Long(00))
end if

if m.Long(04)=h22114411 then
ok=ok+1
else
MsgBox "04: "+hex(m.Long(04))
end if

if m.Long(08)=h11223344 then
ok=ok+1
else
MsgBox "08: "+hex(m.Long(08))
end if

if m.Long(12)=h44332211 then
ok=ok+1
else
MsgBox "12: "+hex(m.Long(12))
end if

if m.Long(16)=h11223344 then
ok=ok+1
else
MsgBox "16: "+hex(m.Long(16))
end if

if ok=5 then
MsgBox "OK"
else
MsgBox "Fail"
end if
Notes:
It should do something like this:

dim m as memoryBlock

for i=0 to numbytes step 2
m.long(i)=EndianSwap32MBS(m.long(i))
next

Feedback, Comments & Corrections

MemoryBlock.SwapBytesMBS(offset as Integer, numBytes as Integer)
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
method MemoryBlock MBS Util Plugin Yes Yes Yes Yes No
Function: Swaps bytes inside the given bounds inside the memoryblock.
Notes: Reverses the order of the bytes at the given offset and length in the memoryBlock. This is helpful to change representation of values from Little Endian (used in Windows) to Big Endian (used in Mac OS) and vice versa. Thanks to Franco Vaccari for the code of this routine.

Feedback, Comments & Corrections

Previous items

The items on this page are in the following plugins: MBS Util Plugin.




Links
MBS Xojo blog