I have two Byte Array (or more) and I should merge it into one
B4X:
Dim A() As Byte = Array As Byte(23,45,67)
Dim B() As Byte = Array As Byte(223,1,70)
Dim c() As Byte
c= A & B ' Come si può fare?
' I want to get it: C = (23,45,67,223,1,70)
This operation I have to repeat it for several Array. At the main Buffer (Byte) I have to add other Byte arrays. How can it be done?
NOTE:
I have already used the ByteConverter library,
ByteConverter.ArrayCopy(OtherArray,0,BufferByte,BufferByte.Length-1,OtherArray
java.lang.ArrayIndexOutOfBoundsException: src.length=17 srcPos=0 dst.length=0 dstPos=-1 length=17
at java.lang.System.arraycopy(Native Method)
at anywheresoftware.b4a.agraham.byteconverter.ByteConverter.ArrayCopy(ByteConverter.java:227)
at b4a.example.bt_printer._addbuffer_writeline(bt_printer.java:262)
at b4a.example.main._printer(main.java:500)
at b4a.example.main._activity_create(main.java:379)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
Dim A() As Byte = Array As Byte(23,45,67)
Dim B() As Byte = Array As Byte(223,1,70)
Dim C(A.Length + B.Length) As Byte
Dim BC As ByteConverter
BC.ArrayCopy(A,0,C,0,A.Length)
BC.ArrayCopy(B,0,C,A.Length,B.Length)
For Each B1 As Byte In C
Log(Bit.And(B1,0XFF)) 'For display as bytes are signed
Next
private Sub Merge(Array1() As Byte, Array2() As Byte) As Object
Private BC As ByteConverter
Dim ByteArray(Array1.Length + Array2.Length) As Byte
BC.ArrayCopy(Array1,0,ByteArray,0,Array1.Length)
BC.ArrayCopy(Array2,0,ByteArray,Array1.Length,Array2.Length)
Return ByteArray
End Sub
I blocked thinking of having to declare an array with an indefinite dimension.
But thanks to your suggestion, the right vision was opened to me.
Now I'm using this sub that is more generic and it will also work on any array (though not byte)
B4X:
private Sub Merge(Array1() As Object, Array2() As Object) As Object
Dim ByteArray(Array1.Length + Array2.Length) As Byte
For i=0 To Array1.Length-1
ByteArray(i)=Array1(i)
Next
For i=0 To Array2.Length-1
ByteArray(Array1.Length+i)=Array2(i)
Next
Return ByteArray
End Sub
BC.ArrayCopy also works on any types of arrays. It will be faster than copying the elements one by one (though the difference will only be significant if you are copying large arrays).
BC.ArrayCopy also works on any types of arrays. It will be faster than copying the elements one by one (though the difference will only be significant if you are copying large arrays).