List or array of byte arrays

Rusty

Well-Known Member
Licensed User
Longtime User
I'm trying to assemble an array containing arrays of bytes.
What is the easiest way to create such an item?
A LIST or multi-dimension byte array?

B4X:
Dim Keys As List = ( "49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54", _
                "56, 57, 48, 49, 50, 51, 52, 53, 54, 27, 72, 203, 109, 216, 28, 118", _
                 "56, 57, 48, 49, 50, 51, 52, 53, 54, 107, 185, 35, 35, 135, 56, 147")
Obviously syntax is incorrect, but am lost as to where to start.
Thanks for any thoughts on this.
Rusty
 

sorex

Expert
Licensed User
Longtime User
try

B4X:
Dim keys() as String
Keys = Array as String ( "49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54", _
                "56, 57, 48, 49, 50, 51, 52, 53, 54, 27, 72, 203, 109, 216, 28, 118", _
                 "56, 57, 48, 49, 50, 51, 52, 53, 54, 107, 185, 35, 35, 135, 56, 147")
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Sorex,
Thanks for your reply.
Although I was unable to create an array or list of byte arrays like I was trying to do, I did implement a version of your suggestion.
Instead of strings, I created a byte array of 10 "groups" of 16 bytes (three displayed in this post) and merely grabbed 16 byte segments from within, based upon an "offset".
This became:
B4X:
Dim keys() as Byte = Array as Byte ( 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, _                56, 57, 48, 49, 50, 51, 52, 53, 54, 27, 72, 203, 109, 216, 28, 118, _
 56, 57, 48, 49, 50, 51, 52, 53, 54, 27, 72, 203, 109, 216, 28, 118, _
               56, 57, 48, 49, 50, 51, 52, 53, 54, 107, 185, 35, 35, 135, 56, 147)
then in code I "grab" the "segment" I want based upon an offset.
B4X:
' look up the encryption key within the integer array
Sub LoadKey(KeyNo As Int) As Byte()
    Dim Ky(16) As Byte                                'create a temporary byte array
    Bconv.ArrayCopy(Keys, KeyNo*16, Ky, 0, 16)        'copy the correct 16 bytes to the array
    Return Ky    
End Sub
The returned Ky contains a byte array of 16 bytes that I use for encryption. This gives me a selectable key value based upon the KeyNo requested. Seems to work well.
I posted this in case someone was wondering if this was solved.
If anyone knows how to make an array of byte arrays, I'd be curious if there is a more elegant way to do this.
Thanks,
Rusty

Note: requires ByteConverter library
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel, this works perfectly.
I had tried a List, but the Array as Byte...syntax I had all wrong.
Regards,
Rusty
 
Upvote 0

bartv

Member
Licensed User
Longtime User
I suppose it's not going to happen...
As far as I know, VB and VBA are the only languages that have that option.
The "standard" is to indicate the number of items (#items) you need in the declaration, and this will give you an array with indexes from 0 to #items-1.
It's possible to declare your array with 1 item more and never use the item at index 0
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hello,
I would like to create programmatically the following command :
B4X:
aStream.Write(Array As Byte (2,Asc("?"),Asc("s"),Asc("t"),3,Asc("3"),Asc("5")))
how can i convert a list to byte array ? i try this :
B4X:
Dim cmdSecv As List
    Dim b() As Byte
    cmdSecv.Initialize
'lblRez.Text= Bit.ToHexString(crc8(cmd))
'calculatedCRC = Bit.ToHexString(crc8(cmd))
'Log(calculatedCRC.Length)
cmdSecv.Add(2)
cmdSecv.Add(Asc("?"))
cmdSecv.Add(Asc("s"))
cmdSecv.Add(Asc("t"))
cmdSecv.Add(3)
cmdSecv.Add(Asc("3"))
cmdSecv.Add(Asc("5"))
    Log(cmdSecv)
'aStream.Write(Array As Byte (2,Asc("?"),Asc("s"),Asc("t"),3,Asc("3"),Asc("5")))
'b=cmdSecv
aStream.Write(Array As Byte (cmdSecv))
 
Upvote 0
Top