13
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?

logigolf
  • 301
  • 1
  • 3
  • 10
  • 3
    Note, that Java and Clojure are using Unicode (UTF-16), not ASCII. You can get things like `(char 0x439) => \й` and `(int \й) => 1081`. – ivant Nov 09 '11 at 08:05

2 Answers2

14

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
BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • I was going to add that you can do math directly on the character, but it looks like you can't in Clojure. Its represented as a java.lang.Character (rather than a char), and Character does not extend java.lang.Number, so the math operators do not work out of the box on characters in Clojure. – BillRobertson42 Nov 08 '11 at 03:38
  • just like with characters primitives in Java - I don't think you can add those either. – amalloy Nov 08 '11 at 03:38
  • Being a newbie in Clojure I have not adapted my mind yet to so many possibilities. Thank you to put me on my way. – logigolf Nov 08 '11 at 03:57
  • 1
    Notice that you can't use this in ClojureScript. In that case you need `(.charCodeAt \A 0)`. See http://stackoverflow.com/questions/37775349/ordinal-int-ascii-value-of-character – Juraj Martinka May 03 '17 at 07:01
12
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"
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70