B4J Question Concatenate Byte Arrays

barx

Well-Known Member
Licensed User
Longtime User
Hi,

I have put this in b4j as it is what I am currently working in, but I'm sure it will be same for b4a.

I get some data through serial. I need to pluck out certain bytes and use them as part of a larger byte array stream later. So I do:

B4X:
Dim mask() As Byte = Array As Byte(serial(9), serial(10), serial(11), serial(12), serial(13), serial(14), serial(15), serial(16), serial(17), serial(18))

This works fine.
As I said I then need to build a larger stream to send back out by concatenating, but I cannot seem to get it to work.

I have tried a few things including

B4X:
astream.Write(Array As Byte(0x55, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, mask))

and

B4X:
astream.Write(Array As Byte(0x55, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a) & mask)
 

stevel05

Expert
Licensed User
Longtime User
Although if you don't need to build a new array, would it not work sending two arrays:

B4X:
astream.Write(Array As Byte(0x55, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a))

astream.Write(mask)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Although if you don't need to build a new array, would it not work sending two arrays:

B4X:
astream.Write(Array As Byte(0x55, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a))

astream.Write(mask)
I don't think it will work as it is 1 command> I don't have control of the other side.

Also, a note. I need to do this in a fashion that will also be possible in b4r as the project is eventually destined for a ESP8266 board. This (b4j) is just the testing stage.

Thanks so far
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ah OK, I don't know what is avallable underlying B4r. The only way I can think of to do it using only arrays is to Dim the array big enough to hold all of the data and copy each byte from both arrays into the correct position.

B4X:
Dim arr1() As Byte = Array As Byte(0x55, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a)
    'Dim arr2() As Byte =  Array As Byte(serial(9), serial(10), serial(11), serial(12), serial(13), serial(14), serial(15), serial(16), serial(17), serial(18))
    Dim arr2() As Byte = Array As Byte(0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a)

    Dim arr(arr1.Length + arr2.Length) As Byte
    For i = 0 To arr1.length -1
        arr(i) = arr1(i)
    Next
    For j = 0 To arr2.length - 1
        arr(i+j) = arr2(j)
    Next

    For i = 0 To arr.Length - 1
        Log(arr(i))
    Next

That should work on all flavours, but it creates a new array which may be an issue if memory is tight and the arrays are large.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use ByteConverter.ArrayCopy to copy arrays of bytes. It will be faster.

I recommend you to go over this code: https://www.b4x.com/android/forum/threads/b4x-class-mjpeg-decoder.73702/#content
It copies the data to a large buffer and uses a global variable to mark the data length. There is also code to "trim" the array. This means that it removes a complete message from the array and move all the other data to the beginning of the array.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
You can use ByteConverter.ArrayCopy to copy arrays of bytes. It will be faster.

I recommend you to go over this code: https://www.b4x.com/android/forum/threads/b4x-class-mjpeg-decoder.73702/#content
It copies the data to a large buffer and uses a global variable to mark the data length. There is also code to "trim" the array. This means that it removes a complete message from the array and move all the other data to the beginning of the array.

I was looking at that Erel but couldn't see anything that suggested ByteConverter was supported by b4r.

I suppose even if it isn't, I can change to the JoinBytes as suggested by @stevel05 above.

cheers
 
Upvote 0
Top