1

How can I convert a string containing an escaped unicode character to a character?

(string-to-char "\\u03C0") evaluates to 92 which is the character value of the first \ in the string. I somehow need to modify the string so I can call (string-to-char "\u03C0") and get 960. (substring "\\u03C0" 1) evaluates to "u03c0" which isn't quite right either!

1

Extract the part after \u with substring, and convert it to a number using base 16:

ELISP> (string-to-number (substring "\\u03C0" 2) 16)
960 (#o1700, #x3c0, ?π)
2

14 characters shorter than legoscia's suggestion :)

(read (format "?%s" "\\u03C0"))
  • 1
    (read (format "?%s" "\\u03C0")) actually seems to be enough! – expez Mar 24 '15 at 13:06
  • Oh, yes, numbers are self-evaluating, so no need to eval it :) – wvxvw Mar 24 '15 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.