Coerce to char
;; can coerce an int (or similar) user=> (char 97) \a ;; a byte can be coerced to a char user=> (let [bytes-array (.getBytes "abc")] (char (first bytes-array))) \a ;; char is just a function user=> (map char [65 66 67 68]) (\A \B \C \D)
;; backslash user=> (char 92) \\ ;; String madness: user=> (map str [\n \newline \\]) ["n" "\n" "\\"] ;; convert back to int: user=> (int \\) 92 ;; whoops! this will expect additional input because it char's the bracket user=> (char \) #=> ;; don't you dare this! user=> (char \ ) \space ;; make it readable user=> (char \space) \space ;; it gets uglier! Mark the text to see the difference user=> \ #=> user=> \ \space ;; MaDnEsS!1! user=> \\\\\\\\ \\ \\ \\ \\
;; string to code round trip ;; input string (def in-string "鬼") ;; single char from string (def in-char (.charAt in-string 0)) ;;=> \鬼 ;; code from char (def code (int in-char)) ;;=> 39740 ;; char from code (def out-char (char code)) ;;=> \鬼 ;; back to input string (def out-string (str out-char)) ;;=> "鬼"
(defn u-sub-char "If n is a single-digit integer in [0, ..., 9], returns the Unicode supscript character for n. Returns nil for other numbers." [n] (if (and (>= n 0) (<= n 9)) (char (+ 0x2080 n)) nil))
char