Emacs: Hexadecimal to Decimal 🚀

By Xah Lee. Date: . Last updated: .

Here's a emacs command that shows the hexadecimal under cursor as decimal.

put this in your Emacs Init File:

(defun xah-hexadecimal-to-decimal ()
  "Prints the decimal value of a hexadecimal string under cursor.

Samples of valid input:

  ffff → 65535
  0xffff → 65535
  #xffff → 65535
  FFFF → 65535
  0xFFFF → 65535
  #xFFFF → 65535

more test cases
  64*0xc8+#x12c 190*0x1f4+#x258
  100 200 300   400 500 600

Command originally written on ~2011-10-03.
URL `http://xahlee.info/emacs/emacs/emacs_show_hexadecimal.html'
Version: 2020-02-17 2024-01-09 2024-02-27"
  (interactive)
  (let (xinput xtemp xp1 xp2)
    (if (region-active-p)
        (setq xp1 (region-beginning) xp2 (region-end))
      (progn
        (save-excursion
          (skip-chars-backward "[:xdigit:]#x")
          (setq xp1 (point))
          (skip-chars-forward "[:xdigit:]#x")
          (setq xp2 (point)))))
    (setq xinput (buffer-substring-no-properties xp1 xp2))
    (let ((case-fold-search nil))
      (setq xtemp (replace-regexp-in-string "\\`0x" "" xinput)) ; C, Perl
      (setq xtemp (replace-regexp-in-string "\\`#x" "" xtemp)) ; elisp
      (setq xtemp (replace-regexp-in-string "\\`#" "" xtemp))  ; CSS
      )
    (message "Radix 16: %s
Radix 10: %d"  xtemp (string-to-number xtemp 16))))

Convert Hexadecimal/Decimal