Last active
August 17, 2023 16:12
-
-
Save nimitmaru/2942f3ce60674632a078fe1c36029fdd to your computer and use it in GitHub Desktop.
ClojureScript utilities for converting between Base64 strings and Uint8Array binary buffers
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 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