logigolf
logigolf

Reputation: 301

How can I get the ascii decimal values of characters in Clojure?

user=> (char 65)
\A
user=> (char 97)
\a
user=> (str (char 65))
"A"
user=> (str (char 97))
"a"

These are the characters from the ascii decimal values ... How do I get the ascii decimal values from the characters?

Upvotes: 13

Views: 10069

Answers (2)

BillRobertson42
BillRobertson42

Reputation: 12883

A character is a number, it's just that clojure is showing it to you as a char. The easiest way is to just cast that char to an int.

e.g.

user=> (int \A)
65
user=> (int (.charAt "A" 0))
65

Upvotes: 15

BLUEPIXY
BLUEPIXY

Reputation: 40145

user=> (doseq [c "aA"] (printf "%d%n" (int c)))
97
65
nil
user=> (map int "aA");;
(97 65)
user=> (apply str (map char [97 65]))
"aA"

Upvotes: 12

Related Questions