Skip to content

Instantly share code, notes, and snippets.

@sritchie
Created March 23, 2011 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sritchie/883338 to your computer and use it in GitHub Desktop.
Save sritchie/883338 to your computer and use it in GitHub Desktop.
(defn little-float
"Converts the supplied byte sequence into a float, in little endian
format."
[bitseq]
(->> bitseq
(map-indexed (fn [idx bit]
(-> (bit-and bit 0xff)
(bit-shift-left (* 8 idx)))))
(reduce bit-xor)
(Float/intBitsToFloat)))
(defn little-float
"Converts the supplied byte sequence into a float, in little endian
format."
[bitseq]
(->> bitseq
(map-indexed (fn [idx bit]
(bit-shift-left
(bit-and bit 0xff)
(* 8 idx))))
(reduce bit-xor)
(Float/intBitsToFloat)))
(defn little-floats
"Converts the supplied byte array into a seq of
little-endian-ordered floats."
[bytes]
(vec (map little-float
(partition float-bytes bytes))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment