Skip to content

Instantly share code, notes, and snippets.

@nimitmaru
Last active August 17, 2023 16:12
Show Gist options
  • Save nimitmaru/2942f3ce60674632a078fe1c36029fdd to your computer and use it in GitHub Desktop.
Save nimitmaru/2942f3ce60674632a078fe1c36029fdd to your computer and use it in GitHub Desktop.
ClojureScript utilities for converting between Base64 strings and Uint8Array binary buffers
(defn uint8array-to-base64 [buffer]
(when-not (nil? buffer)
(let [bytes (js/Uint8Array. buffer)
len (.-byteLength bytes)
binary (->> (range len)
(map #(js/String.fromCharCode (aget bytes %)))
(apply str))]
(js/window.btoa binary))))
(defn base64-to-uint8array [base64]
(when-not (nil? base64)
(-> (js/window.atob base64)
(js/Uint8Array.from
(fn [v] (.charCodeAt v 0))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment