如何在Clojure中初始化字节数组

时间:2022-10-11 21:26:17

What's the syntax for creating a byte array in Clojure initialized to a specified collection of values?

在Clojure中创建字节数组的语法是什么?

Something like this, but that works...

像这样的东西,但那行得通……

(byte-array [0 1 2 3])

(字节数组[0 1 2 3])

4 个解决方案

#1


13  

(byte-array (map byte [0 1 2 3]))

(字节数组(映射字节[0 1 2 3]))

afaik Clojure has no byte literals.

afaik Clojure没有字节文字。

#2


4  

Other posters have given good answers that work well.

其他的海报给出了很好的答案。

This is just in case you are doing this a lot and want a macro to make your syntax a bit tidier:

这只是为了防止您经常这样做,并希望宏使您的语法更整洁:

(defmacro make-byte-array [bytes] 
  `(byte-array [~@(map (fn[v] (list `byte v)) bytes)]))  

(aget (make-byte-array [1 2 3]) 2)
=> 3

#3


1  

(byte-array [(byte 0x00) (byte 0x01) (byte 0x02) (byte 0x03)])

#4


1  

(byte-array [(byte 0) (byte 1) (byte 2)])

Explanation:

解释:

byte creates a byte

字节创建一个字节

byte-array creates a byte[]

字节数组创建一个byte[]

bytes converts it to byte[]

字节转换为字节[]

#1


13  

(byte-array (map byte [0 1 2 3]))

(字节数组(映射字节[0 1 2 3]))

afaik Clojure has no byte literals.

afaik Clojure没有字节文字。

#2


4  

Other posters have given good answers that work well.

其他的海报给出了很好的答案。

This is just in case you are doing this a lot and want a macro to make your syntax a bit tidier:

这只是为了防止您经常这样做,并希望宏使您的语法更整洁:

(defmacro make-byte-array [bytes] 
  `(byte-array [~@(map (fn[v] (list `byte v)) bytes)]))  

(aget (make-byte-array [1 2 3]) 2)
=> 3

#3


1  

(byte-array [(byte 0x00) (byte 0x01) (byte 0x02) (byte 0x03)])

#4


1  

(byte-array [(byte 0) (byte 1) (byte 2)])

Explanation:

解释:

byte creates a byte

字节创建一个字节

byte-array creates a byte[]

字节数组创建一个byte[]

bytes converts it to byte[]

字节转换为字节[]