23

How can I convert hexadecimal to decimal numbers in Emacs calc? For example, if I enter FF, I want it to convert it to 255.

UPDATE: How do I get the reverse operation, turn base 10 to base 16?

Svante
  • 46,788
  • 11
  • 77
  • 118
1.618
  • 779
  • 1
  • 10
  • 16

3 Answers3

38

You can enter any number in the format <base>#<number>. Example: 16#FF is immediately converted to 255.

For the reverse, you need to set the output display mode. In this example, d r 16 RET sets the display to base 16. Set it to base 10 to get the default behaviour again.

By the way, you can also Read The Fine ManualTM: GNU Emacs Calc Manual.

Svante
  • 46,788
  • 11
  • 77
  • 118
  • 3
    `C-x * q 10#255` will show 255 in decimal, hex, octal, and binary in the mini buffer. – Davor Cubranic Sep 13 '16 at 22:58
  • @DavorCubranic this works only if the display radix was decimal to begin with. If it is e.g. binary, it will only show binary. – Arne Dec 09 '19 at 10:02
  • I stumbled on this when trying out your hint. My calc was set to binary display... – Arne Dec 10 '19 at 16:08
13

Svante answered your question, but I'd like to add that the Radix Display Mode change has a quicker keystroke:

  • Show in hexidecimal mode: d 6
  • Show in decimal mode: d 0
Community
  • 1
  • 1
piyo
  • 784
  • 4
  • 14
6

Of course you could type 16#FF to enter 0xFF, but there is a more convenient way.

The other option:

  1. change the display radix to hex with d 6
  2. then enter all the hex numbers you want by prefixing them with a # like #FF and <enter>. (The # means interpret number with given display radix)
  3. After this, change the display radix back to decimal with d 0.

Note: A number entered without # always inserts a decimal number. Note2: This also works the other way round.

Negative Values: Now lets say you have a 8-bit system and you want to know how decimal -3 is stored in this systems RAM.

  1. switch word size: b w 8
  2. enter dec -3 by typing 3 n and <enter>
  3. set display radix to hex with two's complement notation: O d 6. (The O as Option is important to enable two's complement.)

Note: you see 16##FD. Two # means it is signed and the value stored in RAM is 0xFD

The above stuff works also with d 2, d 8 (as shortcuts for bin and oct) and other possible display radixes from 2 to 36 (d r <radix-number>).

This info is taken from the Emacs Calc Manual.

jue
  • 350
  • 3
  • 6
  • 1
    upvoted since you noted that you need to start with a `#` – Swedgin Dec 05 '19 at 10:40
  • When I try to paste a hex value (with no leading `0x`) right after `#` I get `[Bad format]`, but it's easy enough to do #` (pound backtick) to get to the line editor and paste the hex there. – remcycles Jan 09 '20 at 16:46