B4R Question Concating two strings using ArrayCopy and ArrayCop

miker2069

Active Member
Licensed User
Longtime User
FYI, the subject should be:
"Concating two strings using ArrayCopy and ArrayCopy2"

So to begin with I know about JoinStrings, I'm actually trying to avoid JoinStrings since it's generally not a good thing to use in a loop (although I found a work around for that...).

I am attempting to combine a hard coded string prefix to another string into a byte array. For example, I have a string stored in the variable "src". I want to build a byte array "filename" by combine the char/string constant "/code-" and the value in "src". Basically "/code-"+src="/code-comcastmenu".

The code where I try to do this is below:

B4X:
    Dim src As String = "comcastmenu"
    Dim filename(255) As Byte
 
    bc.ArrayCopy("/code-",filename)
    Log("Filename after copied first string:",filename)
    bc.ArrayCopy2(src.GetBytes,0,filename,6,src.Length)
    Log("Filename after appending second string:",filename)

The results are:

Filename after copied first string:/code-
Filename after appending second string:/code-

I never seem to get it to append the value of src into filename. In fact I tried writing my own copy routine like this:

B4X:
Sub CopyArray(src() As Byte, srclen As UInt, trg() As Byte, offset As UInt)
 
    Dim i As UInt
    Dim trg_idx As UInt
    Log("srclen:",srclen)
    Log("offset:",offset)
 
    trg_idx = offset
 
    For i = 0 To srclen - 1
        Log("i=",i, " trg_idx=",trg_idx)
        trg(trg_idx) =  src(i)
        trg_idx = trg_idx + 1
    Next

End Sub

I used the above routine after using bc.ArrayCopy("/code-",filename). However with the above I was always getting out of bound errors on trg array. It seemed to think the trg array was the size of "/code" (the array error always indicated out of bounds with length of 6 or something like that). I suspect an out of bounds error is happening in ArrayCopy2. Is the presence of the null terminated string messing up the appending?
 
Last edited:
Top