Skip to content

Instantly share code, notes, and snippets.

@kno3comma14
Forked from pingles/bytes_to_int.clj
Created November 21, 2022 13:44
Show Gist options
  • Select an option

  • Save kno3comma14/b3921a0fb631ffd562d44708d8818467 to your computer and use it in GitHub Desktop.

Select an option

Save kno3comma14/b3921a0fb631ffd562d44708d8818467 to your computer and use it in GitHub Desktop.
Clojure code to convert a byte array to an integer
(defn bytes-to-int
([bytes]
(bytes-to-int bytes 0))
([bytes offset]
(reduce + 0
(map (fn [i]
(let [shift (* (- 4 1 i)
8)]
(bit-shift-left (bit-and (nth bytes (+ i offset))
0x000000FF)
shift)))
(range 0 4)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment