all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How do I convert hex numbers to decimal ?
@ 2009-09-07  1:03 bolega
  2009-09-07  1:11 ` Janis Papanagnou
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: bolega @ 2009-09-07  1:03 UTC (permalink / raw
  To: help-gnu-emacs; +Cc: gnuist006

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

1) How do I convert hex numbers to decimal ?
2) How do I convert ascii to the character that it represents, short
of a table ? Any function or simpler algorithm ?
3) What are the variables and how to set their values ?

read-quoted-char-radix
quoted-char-radix

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 ?

Thanks a lot to the star who can explain.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  1:03 How do I convert hex numbers to decimal ? bolega
@ 2009-09-07  1:11 ` Janis Papanagnou
  2009-09-07 14:44   ` Wang Lei
  2009-09-07  2:56 ` Eli Zaretskii
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Janis Papanagnou @ 2009-09-07  1:11 UTC (permalink / raw
  To: help-gnu-emacs

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

So why post to comp.unix.shell then?

(Using printf is a shell way of doing what you want.)

> 
> 1) How do I convert hex numbers to decimal ?
> 2) How do I convert ascii to the character that it represents, short
> of a table ? Any function or simpler algorithm ?
> 3) What are the variables and how to set their values ?
> 
> read-quoted-char-radix
> quoted-char-radix
> 
> 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 ?
> 
> Thanks a lot to the star who can explain.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  1:03 How do I convert hex numbers to decimal ? bolega
  2009-09-07  1:11 ` Janis Papanagnou
@ 2009-09-07  2:56 ` Eli Zaretskii
       [not found] ` <mailman.6147.1252292305.2239.help-gnu-emacs@gnu.org>
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2009-09-07  2:56 UTC (permalink / raw
  To: help-gnu-emacs, gnuist006

> From: bolega <gnuist006@gmail.com>
> Date: Sun, 6 Sep 2009 18:03:57 -0700 (PDT)
> Cc: gnuist006@gmail.com
> 
> 1) How do I convert hex numbers to decimal ?

I presume you mean for external representation purposes, in which case
just use the %d format.  Otherwise, reading a hex number converts it
automatically, so you don't need to bother about the internal
representation.

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

Use the %d format again.

> 3) What are the variables

Type "C-h v read-quoted-char-radix RET" etc. and you will be shown the
documentation of the variable.

> and how to set their values ?

"M-x set-variable RET read-quoted-char-radix RET <value> RET"

> 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 ?

"C-k C-x =".

Sounds like you should read the basic parts of the Emacs manual and
the Elisp manual, before you start coding in Lisp.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
       [not found] ` <mailman.6147.1252292305.2239.help-gnu-emacs@gnu.org>
@ 2009-09-07  5:52   ` bolega
  2009-09-07  6:38     ` Teemu Likonen
  0 siblings, 1 reply; 8+ messages in thread
From: bolega @ 2009-09-07  5:52 UTC (permalink / raw
  To: help-gnu-emacs; +Cc: gnuist006

On Sep 6, 7:56 pm, Eli Zaretskii <e...@gnu.org> wrote:
> > From: bolega <gnuist...@gmail.com>
> > Date: Sun, 6 Sep 2009 18:03:57 -0700 (PDT)
> > Cc: gnuist...@gmail.com


Thanks for your reply but my questions remain and I explain better
this time.

>
> > 1) How do I convert hex numbers to decimal ?
>
> I presume you mean for external representation purposes, in which case
> just use the %d format.  Otherwise, reading a hex number converts it
> automatically, so you don't need to bother about the internal
> representation.

What I want is a function like this:

(function "%1F)


and I do C-x C-e and it executes and calculates the decimal value and
returns or prints it.

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

what I want is a function like this:

(function "%3F")              and it converts it and returns or
prints ? either an existing facility or combination thereof.


> > 3) What are the variables
>
> Type "C-h v read-quoted-char-radix RET" etc. and you will be shown the
> documentation of the variable.

The documentation did not have an example of usage and contexts. I had
used describe-variable before.

> > and how to set their values ?
>
> "M-x set-variable RET read-quoted-char-radix RET <value> RET"

Can i use the following ?

(set "read-quoted-char-radix" 16)

or

(setq "read-quoted-char-radix" 16)


and which one and why ?

> > 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 ?
>
> "C-k C-x =".

Thanks.

> Sounds like you should read the basic parts of the Emacs manual and
> the Elisp manual, before you start coding in Lisp.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  5:52   ` bolega
@ 2009-09-07  6:38     ` Teemu Likonen
  0 siblings, 0 replies; 8+ messages in thread
From: Teemu Likonen @ 2009-09-07  6:38 UTC (permalink / raw
  To: help-gnu-emacs

On 2009-09-06 22:52 (-0700), bolega wrote:

> What I want is a function like this:
>
> (function "%1F)

Where that "%" comes from? Do you want your function to require it?
Here's an example:

    (defun my-hex-to-int (hexstring)
      (car (read-from-string (replace-regexp-in-string
                              "\\`%" "#x" hexstring))))

    (my-hex-to-int "%1F")
    => 31

Or without the % prefix:

    (defun my-hex-to-int2 (hexstring)
      (car (read-from-string (concat "#x" hexstring))))

    (my-hex-to-int2 "1F")
    => 31

> what I want is a function like this:
>
> (function "%3F")              and it converts it and returns or
> prints ? either an existing facility or combination thereof.

Do you mean like this?

    (format "%c" (my-hex-to-int "%3F"))
    => "?"

It returns a string containing that character.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  1:03 How do I convert hex numbers to decimal ? bolega
                   ` (2 preceding siblings ...)
       [not found] ` <mailman.6147.1252292305.2239.help-gnu-emacs@gnu.org>
@ 2009-09-07  7:12 ` Pascal J. Bourguignon
  2009-10-05  7:05 ` josephoswald+gg@gmail.com
  4 siblings, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-09-07  7:12 UTC (permalink / raw
  To: help-gnu-emacs

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__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  1:11 ` Janis Papanagnou
@ 2009-09-07 14:44   ` Wang Lei
  0 siblings, 0 replies; 8+ messages in thread
From: Wang Lei @ 2009-09-07 14:44 UTC (permalink / raw
  To: help-gnu-emacs

You could have a look at  format function.

On 9/7/09, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> bolega wrote:
>> I have a bunch of related questions, but the solution is desired in
>> emacs lisp and to work inside emacs only.
>
> So why post to comp.unix.shell then?
>
> (Using printf is a shell way of doing what you want.)
>
>>
>> 1) How do I convert hex numbers to decimal ?
>> 2) How do I convert ascii to the character that it represents, short
>> of a table ? Any function or simpler algorithm ?
>> 3) What are the variables and how to set their values ?
>>
>> read-quoted-char-radix
>> quoted-char-radix
>>
>> 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 ?
>>
>> Thanks a lot to the star who can explain.
>


-- 
Regards
Lei




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How do I convert hex numbers to decimal ?
  2009-09-07  1:03 How do I convert hex numbers to decimal ? bolega
                   ` (3 preceding siblings ...)
  2009-09-07  7:12 ` Pascal J. Bourguignon
@ 2009-10-05  7:05 ` josephoswald+gg@gmail.com
  4 siblings, 0 replies; 8+ messages in thread
From: josephoswald+gg@gmail.com @ 2009-10-05  7:05 UTC (permalink / raw
  To: help-gnu-emacs

On Sep 6, 9:03 pm, bolega <gnuist...@gmail.com> wrote:
> I have a bunch of related questions, but the solution is desired in
> emacs lisp and to work inside emacs only.
>
> 1) How do I convert hex numbers to decimal ?
> 2) How do I convert ascii to the character that it represents, short
> of a table ? Any function or simpler algorithm ?
> 3) What are the variables and how to set their values ?
>
> read-quoted-char-radix
> quoted-char-radix
>
> 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 ?
>
> Thanks a lot to the star who can explain.

Emacs Lisp has

     (string-to-number string &optional base)

     Parse string as a decimal number and return the number.
     This parses both integers and floating point numbers.
     It ignores leading spaces and tabs.

     If base, interpret string as a number in that base.  If base
isn't
     present, base 10 is used.  base must be between 2 and 16
(inclusive).
     If the base used is not 10, floating point is not recognized.

I found this in the Elisp Info documentation under String Conversion.

In your case you want to use (string-to-number "f00" 16)


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-10-05  7:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-07  1:03 How do I convert hex numbers to decimal ? bolega
2009-09-07  1:11 ` Janis Papanagnou
2009-09-07 14:44   ` Wang Lei
2009-09-07  2:56 ` Eli Zaretskii
     [not found] ` <mailman.6147.1252292305.2239.help-gnu-emacs@gnu.org>
2009-09-07  5:52   ` bolega
2009-09-07  6:38     ` Teemu Likonen
2009-09-07  7:12 ` Pascal J. Bourguignon
2009-10-05  7:05 ` josephoswald+gg@gmail.com

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.