Android Code Snippet [B4X] Join arrays of bytes

Discussion in 'Code Snippets' started by Erel, Dec 3, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Input = A list (or array of objects) with one or more arrays of bytes.
    Output = An array of bytes with the input data.

    Code:
    Sub JoinBytes(ListOfArraysOfBytes As ListAs Byte()
       
    Dim size As Int
       
    For Each b() As Byte In ListOfArraysOfBytes
         size = size + b.Length
       
    Next
       
    Dim result(size) As Byte
       
    Dim index As Int
       
    Dim bc As ByteConverter 'ByteConverter library
       For Each b() As Byte In ListOfArraysOfBytes
         bc.ArrayCopy(b, 
    0, result, index, b.Length)
         index = index + b.Length
       
    Next
       
    Return result
    End Sub
    Usage example:
    Code:
    Dim b() As Byte = JoinBytes(Array("abcde".GetBytes("utf8"), "fghij".GetBytes("utf8"), _
       
    "klmnop".GetBytes("utf8")))
    Log(BytesToString(b, 0, b.Length, "utf8"))
    Depends on ByteConverter library (iRandomAccessFile in B4i).
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice