Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display numbers in different bases under elisp?

Tags:

emacs

elisp

As we know, elisp supports number in different bases, e.g. #20r1j equals to 39 in base-10. I want to display #20r1j as #20r1j. But (format "%d" #20r1j) gives me 39. How to keep the number in its original base?

like image 790
Vivodo Avatar asked Apr 28 '12 02:04

Vivodo


1 Answers

As a format string, you are quite limited in the bases you can display:

%d means print as number in decimal (%o octal, %x hex).
%X is like %x, but uses upper case.

You can use the calc library to manage this for you, however:

(require 'calc-bin)

(let ((calc-number-radix 20))
  (math-format-radix 39))
"1J"

(let ((calc-number-radix 20))
  (math-format-radix #20r1j))
"1J"

As with the read syntax you're using, allowed values of calc-number-radix run from 2 to 36.

like image 153
phils Avatar answered Nov 15 '22 21:11

phils