B4J Question VB6 CopyMemory Alternative for Arrays/Variables Mix

Tobias Leininger

Member
Licensed User
Longtime User
Hi, I know there have been some topics but I'm curious if there is a more efficient solution to my problem.

As most of you probably know in VB6 CopyMemory is a very fast and efficient way to copy bytes between different data types.
I use it frequently to fill arrays with data or get data out of arrays into variables.

One of my use cases would be sth. like this:
VB6 CopyMemory:
Public Function DecodeFullHPS3DMessage(ByRef MessagePayload() As Byte, ByRef AvgDist As Long, ByRef MaxDist As Long, ByRef MinDist As Long, ByRef DepthData() As Byte)
  
    AvgDist = 0
    CopyMemory AvgDist, MessagePayload(2), 2
    MaxDist = 0
    CopyMemory MaxDist, MessagePayload(12), 2
    MinDist = 0
    CopyMemory MinDist, MessagePayload(14), 2
  
    ReDim DepthData(19199)
    CopyMemory DepthData(0), MessagePayload(24), 19200
End Function
As you can see it is super simple to split data from a serial stream (held in a byte array) into its components.

In B4X I'm not sure what the best way to do this would be, I'm trying sth. like this, which works but is kind of a mess:
B4X:
    Private bytes4(4) As Byte

        bytes4(3) = MessagePayload(2)
        bytes4(2) = MessagePayload(3)
        AvgDist = BC.intsFromBytes(bytes4)(0)

        bytes4(3) = MessagePayload(12)
        bytes4(2) = MessagePayload(13)
        MaxDist = BC.intsFromBytes(bytes4)(0)

        bytes4(3) = MessagePayload(14)
        bytes4(2) = MessagePayload(15)
        MinDist = BC.intsFromBytes(bytes4)(0)

        Private DepthData(19200) As Byte
        BC.ArrayCopy(MessagePayload, 24, DepthData, 0, 19200)

Any recommendations?
 

Tobias Leininger

Member
Licensed User
Longtime User
Thanks for the reply. I will have a closer look at RandomAccessFile.Initialize3 .

I have to interface numerous devices that I don't have control over, so changing the protocol is impossible.

There is no way to implement anything similar to CopyMemory as a very efficient data transfer method between all kinds of variables?
In this example I get about one Megabyte of data per second so it should not be too inefficient.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no way to implement anything similar to CopyMemory as a very efficient data transfer method between all kinds of variables?
In this example I get about one Megabyte of data per second so it should not be too inefficient.
1. Make sure to test it in release mode.

2. If it not fast enough then please upload a project with the code.
 
Upvote 0
Top