Created
October 3, 2012 14:52
-
-
Save emdeesee/3827341 to your computer and use it in GitHub Desktop.
Encode and decode "binary" strings with Clojure
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
(use '[clojure.string :only [trim replace]] | |
'[clojure.pprint :only [cl-format] :rename {cl-format format}]) | |
(defn- unspace [s] | |
(trim (replace s " " ""))) | |
(defn decode [bitstring] | |
"\"01100110 01101111 01101111\" -> \"foo\" | |
Whitespace in the input string is ignored." | |
(->> (partition 8 (unspace bitstring)) | |
(map #(char (Integer/parseInt (apply str %) 2))) | |
(apply str) | |
(trim))) | |
(defn encode [s] | |
"\"foo\" -> \"01100110 01101111 01101111\"" | |
(trim (format nil "~{~8,'0b ~}" (map #(int %) s)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll look in to creating a lib thanks for this gist