Emacs Lisp: getting ascii value of character

让人想犯罪 __ 提交于 2019-12-07 01:27:52

问题


I'd like to translate a character in Emacs to its numeric ascii code, similar to casting char a = 'a'; int i = (int)a in c. I've tried string-to-number and a few other functions, but none seem to make Emacs read the char as a number in the end.

What's the easiest way to do this?


回答1:


String is an array.

(aref "foo" 0)



回答2:


To get the ascii-number which represents the character --as Drew said-- put a question mark before the character and evaluate that expression

?a ==> 97

Number appears in minibuffer, with C-u it's written behind expression.

Also the inverse works

(insert 97) will insert an "a" in the buffer.

BTW In some cases the character should be quoted

?\" will eval to 34




回答3:


A character is a whole number in Emacs Lisp. There is no separate character data type.

Function string-to-char is built-in, and does what you want. (string-to-char "foo") is equivalent to (aref "foo" 0), which is @abo-abo's answer --- but it is coded in C.



来源:https://stackoverflow.com/questions/19862517/emacs-lisp-getting-ascii-value-of-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!