Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 1.02 KB

integer-to-character-conversions.asciidoc

File metadata and controls

47 lines (32 loc) · 1.02 KB

Converting Between Characters and Integers

by Ryan Neufeld

Problem

You need to convert characters to their respective code points (integer values) or vice versa.

Solution

Use the int function to coerce a character to its integer value.

(int \a)
;; -> 97

(int \ø)
;; -> 248

(map int "Hello, world!")
;; -> (72 101 108 108 111 44 32 119 111 114 108 100 33)

Use the char function to coerce an integer (or any other number type) back into a character.

(char 97)
;; -> \a

(char 125)
;; -> \}

(reduce #(str %1 (char %2))
        ""
        [115 101 99 114 101 116 32 109 101 115 115 97 103 101 115])
;; -> "secret messages"

Discussion

Converting between characters and integers is a simple matter in Clojure; merely use the int and char functions to coerce characters between their numerical and character forms.

See Also