Skip to content

Instantly share code, notes, and snippets.

@emdeesee
Created October 3, 2012 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emdeesee/3827341 to your computer and use it in GitHub Desktop.
Save emdeesee/3827341 to your computer and use it in GitHub Desktop.
Encode and decode "binary" strings with Clojure
(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))))
@educationofjon
Copy link

I'll look in to creating a lib thanks for this gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment