Android Question Initialising byte array

Hamo

Member
Licensed User
Longtime User
I am trying to declare and initialize a byte array like this

dim ba(4) as byte = 1,2,3,4

Is there a way of doing this without
ba(0) = 1
ba(1) = 2
ba(2) = 3
ba(3) = 4

Hamo
 

sorex

Expert
Licensed User
Longtime User
in B4A you need to do it in 2 steps.

this might work

B4X:
dim ba() as byte
ba=array as byte (1,2,3,4)
 
Upvote 0

Hamo

Member
Licensed User
Longtime User
This works but I was looking for a more direct way

Dim bc As ByteConverter
Dim s As String
dim PacketToSend(16) as byte

s = "83c20014ffffff00ffffff00ffffff00"
PacketToSend = bc.HexToBytes(s)

Hamo
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
thanks for the hint, Erel.

I remember that we discussed this on the chat 2.5 years ago and we couldn't get it to work like that.
So I kept using that 2 step method.

It must've been that 2nd byte definition that we didn't use and used

B4X:
Dim b() As Byte = (1, 2, 3, 4, 0xff, 0xab)

What's the reason to define the byte (and array) twice?
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
but in a lot of languages () already means it's an array.
I, and probably others aswell, read that line as "b as array of bytes=array as byte(1,2..)"

so it's declared twice.

b is declared to expect bytes so the "array as byte" is kind of overkill. (unless it opens doors for some wicked stuff)
 
Upvote 0
Top