Emacs: Command to Convert Hexadecimal Decimal 📜

By Xah Lee. Date: . Last updated: .

decimal to hexadecimal

put this in your Emacs Init File:

(defun xah-decimal-to-hexadecimal ()
  "Convert the decimal under cursor to hexadecimal.
URL `http://xahlee.info/emacs/emacs/emacs_show_hexadecimal.html'
Created: 2025-02-26
Version: 2025-02-26"
  (interactive)
  (let (xbeg xend xinput)
    (skip-chars-backward "0-9")
    (setq xbeg (point))
    (skip-chars-forward "0-9")
    (setq xend (point))
    (setq xinput (buffer-substring-no-properties xbeg xend))
    (delete-region xbeg xend)
    (insert (format "%x" (string-to-number xinput)))))

hexadecimal to decimal

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

Samples of valid input (all return 255):

  #xFF
  #xff
  0xFF
  0xff
  FF
  ff

Command originally written on ~2011-10-03.

URL `http://xahlee.info/emacs/emacs/emacs_show_hexadecimal.html'
Created: 2020-02-17
Version: 2026-01-04"
  (interactive)
  (let (xbeg xend)
    (if (region-active-p)
        (setq xbeg (region-beginning) xend (region-end))
      (progn
        (save-excursion
          (skip-chars-backward "[:xdigit:]#x")
          (setq xbeg (point))
          (skip-chars-forward "[:xdigit:]#x")
          (setq xend (point)))))
    (when (eq xbeg xend) (user-error "%s error, input is empty." this-command))

    (let ((xnum (let ((case-fold-search nil))
                  (replace-regexp-in-string "\\`0x\\|\\`#x\\|\\`#" "" (buffer-substring-no-properties xbeg xend)))))
      (message
       "%s
Hexadecimal is: %s
Decimal is: %d"
       this-command
       xnum (string-to-number xnum 16)))))

Convert Hexadecimal/Decimal