2011-10-01

Emacs: Converting Decimal and Hexadecimal

Sometimes i need to convert between decimal and hexadecimal. Here's how to do that using emacs's built-in calculator.

  • Call “calc” 【Alt+x】.
  • Type any number. For example, 10.
  • Type “d6” to turn the display into hexadecimal form.
  • Type “d0” to turn the display into decimal form.

To type a hex number, type #, then type “16#aa” for the hex “aa”.

Another way, i find simpler, is using elisp. Open a new file, then type the following:

(format "%x" 10)  ; decimal to hex. Returns 「a」
(format "%d" #xa) ; hex 「a」 to decimal. Returns 「10」.

Select the code, then call “eval-region” 【Alt+x】. Or, put cursor at the end of the right parenthesis, then call “eval-last-sexp” 【Ctrl+x Ctrl+e】 (See: Emacs: How to Eval Elisp Code, Find Functions, Search Documentation.)

To open a new file in ErgoEmacs, press 【Ctrl+n】. In GNU Emacs, call “switch-to-buffer” 【Ctrl+x b】 then type a new name.