help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I convert hex numbers to decimal ?


From: Pascal J. Bourguignon
Subject: Re: How do I convert hex numbers to decimal ?
Date: Mon, 07 Sep 2009 09:12:54 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

bolega <gnuist006@gmail.com> writes:

> I have a bunch of related questions, but the solution is desired in
> emacs lisp and to work inside emacs only.

Then do not post in the irrelevant newsgroups!


> 1) How do I convert hex numbers to decimal ?

Wrong question.

Numbers are not hex or decimal.  Numbers are.

Now, you can represent a number with a base system, such as base
sixteen or base ten.  And of course, you can convert back a
representation in a give base into a number.

http://en.wikipedia.org/wiki/Positional_notation
http://groups.google.com/group/comp.programming/browse_thread/thread/5da7ce38ff66d160/f6088718a82956dd?q=convert+base+author:Pascal+author:Bourguignon#f6088718a82956dd

(You can use the function convert-to-base and convert-from-base from
that post, just (require 'cl) and remove the colon before the keywords
of the loops).


> 2) How do I convert ascii to the character that it represents, short
> of a table ? Any function or simpler algorithm ?

Wrong question.

ASCII *is* the convertion table.  You cannot convert a table into a
single character.

ASCII defines the one-to-one mapping between codes (numbers) between 0
and 127, and the union of a set of control functions and a set of
characters.


In emacs, characters are represented by their code value, which, in
the case of the characters in the ASCII character set, is their ASCII
code.  So, in emacs, like in C, you do not need any function to
convert between them.

Or, you can write:

  (defun char-code (char) char)
  (defun code-char (code) code)

  (format "code = %d" (char-code ?a)) --> "code = 97"
  (format "char = %c" (code-char 97)) --> "char = a"




> 3) What are the variables and how to set their values ?
>
> read-quoted-char-radix
> quoted-char-radix

You can fetch the documentation of a variable by typing C-h v.
Move the cursor on one variable, and type C-h v RET
or type C-h v and the name of the variable RET

If the variable is customizable, you can change its value with

  (customize-variable 'variable)

otherwise you can always set it:

  (require 'cl)
  (setf variable value)



> 4) How do i find the emacs lisp code for C-x = to see how it gives the
> ascii of the char at the cursor and then use to find the reverse, ie
> ascii to char ?

You can find the command bound to a keychord with C-h k followed by the 
keychord.

So, in this case: C-h k C-x =


-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]