Created
July 21, 2010 10:12
-
-
Save dsp/484301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn base32enc | |
"Returns a base32 encoded strings of the byte vector | |
A base32 character can represent 5 bits. We split the vector | |
into chunks of 5 bytes = 40 bits = 8 * 5 bits and create a 40 bit | |
Long representation of the 5 bytes. We then use Long/toString to | |
get the 8 characters base32 representation. | |
" | |
([vec] | |
(base32enc vec *base32-alpha*)) | |
([vec alpha] | |
(mapcat | |
(fn [coll] | |
(let [res (reduce | |
#(bit-or (bit-shift-left %1 8) %2) | |
(map #(bit-and 0xff %) coll))] | |
(loop [bitcount (* 8 (count vec)) | |
s "" | |
res res] | |
(if (<= bitcount 0) s | |
(recur (- bitcount 5) ; rebind bitcount | |
(str (get alpha (bit-and res 0x1f)) s) | |
(bit-shift-right res 5)))))) | |
(partition-all 5 vec)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment