2

I have a character, e.g. O, and I want to convert it to its hexadecimal encoded value. The result must be 4f.

How can I do this in Emacs 26.1?

NickD
  • 27,023
  • 3
  • 23
  • 42
a_subscriber
  • 3,854
  • 1
  • 17
  • 47

2 Answers2

4

You can simply run M-x eval-expression and type ?O. You will get the following output in the minibuffer:

79 (#o117, #x4f, ?O)
  • `C-x =` (`what-cursor-position`) works as well, it prints something like "Char: O (79, #o117, #x4f) point=146 of 146 (99%) column=0". – xuchunyang Jun 20 '19 at 23:31
  • Nice, I didn't know about `?0`. A side note, by default `eval-expression` is bounded to `M-:` which make it much faster... – Pouya Jun 21 '19 at 12:47
1

Maybe no so elegant but should do the trick. Note I have assigned it to "C-." which might be used by other functions in your setup. Reassign as you wish:

(defun hexify()
  (interactive)
  (setq hx (format "%x" (char-before)))
  (delete-backward-char 1)
  (insert hx))

(global-set-key (kbd "C-.") 'hexify)

Calling the function, replaces the character before the cursor with its hex. If you just want the value Arkadiusz's answer will do.

Pouya
  • 203
  • 1
  • 10