Android Question How to combine 2 bytes?

hongbii khaw

Member
Licensed User
Longtime User
Hi all,
I have a question in byte converting,
Let's say i have a
B4X:
Dim byte1() As Byte = Array As Byte(0x01,0x02,0x03)
and
B4X:
Dim byte2() As Byte = Array As Byte(0x04,0x05,0x06)
Any method for me to combine them to become something like
B4X:
byte_combine() = (0x01,0x02,0x03,0x04,0x05,0x06)
i tried this
B4X:
    Dim byte1() As Byte = Array As Byte(0x01,0x02,0x03)
    Dim byte2() As Byte = Array As Byte(0x04,0x05,0x06)
    Dim byte_combine(byte1.length+byte2.length) As Byte
    For i=0 To byte1.length+byte2.length-1
        If i<byte1.length Then
            byte_combine(i)=byte1(i)
        Else
            byte_combine(i)=byte2(i-byte1.length)
        End If
    Next
But when the number of byte that need to add up getting more, the coding is very long,
Any other solution for this ?

Thank you.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Any other solution for this ?
B4X:
Sub CombineBytes(arr1() As Byte, arr2() As Byte) As Byte()
   Dim res(arr1.Length + arr2.Length) As Byte
   Dim bc As ByteConverter 'ByteConverter library
   bc.ArrayCopy(arr1, 0, res, 0, arr1.Length)
   bc.ArrayCopy(arr2, 0, res, arr1.Length, arr2.Length)
   Return res
End Sub

Dim combined() As Byte = CombineBytes(byte1, byte2)
 
Upvote 0
Top