all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Mirko <mvukovic@nycap.rr.com>
To: help-gnu-emacs@gnu.org
Subject: Re: converting numbers to strings in arbitrary base (up to 36)
Date: Tue, 15 Mar 2011 13:57:48 -0700 (PDT)	[thread overview]
Message-ID: <2c109eb6-3bf3-430b-91cf-5aed4e25dc64@glegroupsg2000goo.googlegroups.com> (raw)
In-Reply-To: <87tyf4mbzo.fsf@kuiper.lan.informatimago.com>

On Tuesday, March 15, 2011 3:15:23 PM UTC-4, Pascal J. Bourguignon wrote:
> Mirko <mvuk...@nycap.rr.com> writes:
> 
> > This is an elisp question:
> >
> > I can specify integers in base 2-36 using #xyr..., and I can read them from a file.
> >
> > But is there a way to write an integer in arbitrary base (again, 2-36) to a file?
> >
> > I looked at string-to-number, format, and calc
> >
> > In greater detail,
> >
> > I have a file where I want to keep a counter (in base 36).
> > When needed, I want to open the file, read the number.
> > Occasionally, I want to increment the number, and write it back out.
> >
> > I can handle reading and writing :-).  It is writing the base 36 that
> > I don't know how to handle.
> 
> 
> For bases eight, ten, and sixteen, you can use format:
> 
>     (format "#8r%o #10r%d #16r%x" 42 42 42)
>     --> "#8r52 #10r42 #16r2a"
> 
> 
> 
> For the other bases, you must implement it yourself, or use
> integer-to-base (from 
> http://git.informatimago.com/viewgit/index.php?a=viewblob&p=public/emacs&h=94650b080e8a38e9620687b9abd3fcb3e24c6561&hb=e57067965e5def38d5fa18dab0aa75cff3d049b9&f=pjb-utilities.el
> )
> 
> 
> (loop for b from 2 to 36
>       collect (format  "#%dr%s" b (integer-to-base 42 b)))
> 
> 
> --> ("#2r101010" "#3r1120" "#4r222" "#5r132" "#6r110" "#7r60" "#8r52"
>      "#9r46" "#10r42" "#11r39" "#12r36" "#13r33" "#14r30" "#15r2C"
>      "#16r2A" "#17r28" "#18r26" "#19r24" "#20r22" "#21r20" "#22r1K"
>      "#23r1J" "#24r1I" "#25r1H" "#26r1G" "#27r1F" "#28r1E" "#29r1D"
>      "#30r1C" "#31r1B" "#32r1A" "#33r19" "#34r18" "#35r17" "#36r16")
> 
> 
> 
> 
> (defun integer-to-base (decimal base &optional width padchar
>                                 commachar comma-interval)
>   "
> DO:      Convert  a decimal  value into  a string  contening the 
>          same value expressed into the given base. 1<base<37.
>          The optional WIDTH specifies the minimum length of the returned 
>          string (0-left-filled), not counting a '-' sign.
> SEE-ALSO:float-to-base."
>     ;;TODO: Implement commachar, comma-interval
>   (cond
>    ((not (integerp base))         (error "Invalid base (%S)." base))
>    ((or (< base 2) (< 36 base))   (error "Invalid base (%d)." base))
>    ((not (integerp decimal))      
>     (error "For now, I only convert integer values.")))
>   (let ((digits "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
>         (buffer (make-string 32 ?0))
>         (b 31)
>         (sign "")
>         )
>     (if (< decimal 0)
>         (setq sign "-"
>               decimal (abs decimal)))
>     (while (< 0 decimal)
>       (let ((digit   (% decimal base)))
>         (aset buffer b (aref digits digit))
>         (setq decimal (/ (- decimal digit) base)
>               b       (- b 1))))
>     (if width (if (< 32 width)
>                   (setq sign (concat sign (make-string (- width 32) padchar))
>                         width 32)))
>     (concat sign 
>             (if (and width (< (- 32 width) (+ 1 b) ))
>                 (substring buffer (- 32 width))
>               (if (= b 31) "0" (substring buffer (+ 1 b)))))))
> 
> 
> 
> 
> 
> 
> -- 
> __Pascal Bourguignon__                     http://www.informatimago.com/
> A bad day in () is better than a good day in {}.

Thanks to everyone

Mirko



  reply	other threads:[~2011-03-15 20:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-15 18:42 converting numbers to strings in arbitrary base (up to 36) Mirko
2011-03-15 19:15 ` Pascal J. Bourguignon
2011-03-15 20:57   ` Mirko [this message]
2011-03-15 19:39 ` Stefan Monnier
2011-03-15 20:47 ` Johan Bockgård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2c109eb6-3bf3-430b-91cf-5aed4e25dc64@glegroupsg2000goo.googlegroups.com \
    --to=mvukovic@nycap.rr.com \
    --cc=gnu.emacs.help@googlegroups.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.