byte-array

added
1.1

ns
clojure.core

type
function

(byte-array size-or-seq) (byte-array size init-val-or-seq)

Creates an array of bytes

                ;; create an array of bytes
;; and demonstrate that you can use it in the standard Java fill function
;; note the needed byte coercion in the fill function call

user=> (def bees (byte-array 10))
#'user/bees

user=> (for [i (range 10)](aset-byte bees i (* i i)))
(0 1 4 9 16 25 36 49 64 81)

user=> (vec bees)
[0 1 4 9 16 25 36 49 64 81]

user=> (java.util.Arrays/fill bees (byte 122))
nil
user=> (vec bees)
[122 122 122 122 122 122 122 122 122 122]
user=>
            
                ;; copied from the example in clojure.core/byte
user=> (def x (byte-array [(byte 0x43) 
                           (byte 0x6c)
                           (byte 0x6f)
                           (byte 0x6a)
                           (byte 0x75)
                           (byte 0x72)
                           (byte 0x65)
                           (byte 0x21)]))
#'user/x

user=> (String. x)
"Clojure!"