Android Question Initialising byte array

Discussion in 'Android Questions' started by Hamo, Feb 21, 2015.

  1. Hamo

    Hamo Member Licensed 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
     
  2. sorex

    sorex Expert Licensed User

    in B4A you need to do it in 2 steps.

    this might work

    Code:
    dim ba() as byte
    ba=
    array as byte (1,2,3,4)
     
  3. Hamo

    Hamo Member Licensed 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
     
  4. sorex

    sorex Expert Licensed User

    that needs an additional library, mine doesn't.
     
  5. Hamo

    Hamo Member Licensed User

    Thanks, I'll give it a try.
    Hamo
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You need one step:
    Code:
    Dim b() As Byte = Array As Byte(12340xff0xab)
     
    Last edited: Feb 22, 2015
    RandomCoder and ellpopeb4a like this.
  7. sorex

    sorex Expert Licensed 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

    Code:
    Dim b() As Byte = (12340xff0xab)
    What's the reason to define the byte (and array) twice?
     
    Last edited: Feb 22, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim i As Int = 5
    We declare a variable of type int and assign the value 5 to this variable.

    Code:
    Dim b() As Byte = Array As Byte(12340xff0xab)
    We declare a variable of type "array of bytes" and assign an array of bytes to it.
     
  9. sorex

    sorex Expert Licensed 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)
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Array As <type>(...) is not a declaration. It is a shorthand syntax to create arrays.

    You can use it in any place that expects an array (or list).

    Code:
    For Each i As Int In Array As Int(12,3)
     
    Log(i)
    List1.AddAll(Array As String("a", "b"))
     
    emexes likes this.
  11. Hamo

    Hamo Member Licensed User

    Great stuff Erel many thanks
    Hamo
     
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