From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: converting numbers to strings in arbitrary base (up to 36) Date: Tue, 15 Mar 2011 20:15:23 +0100 Organization: Informatimago Message-ID: <87tyf4mbzo.fsf@kuiper.lan.informatimago.com> References: <56262053-76f2-494e-b7ed-63a9105229ee@glegroupsg2000goo.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1300218061 575 80.91.229.12 (15 Mar 2011 19:41:01 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 15 Mar 2011 19:41:01 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 15 20:40:57 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Pza6z-0006jF-4L for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Mar 2011 20:40:57 +0100 Original-Received: from localhost ([127.0.0.1]:45828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pza6y-0006Mk-JH for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Mar 2011 15:40:56 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 90 Original-X-Trace: individual.net tG8LLgeL1NiQIv5hdbbUngPXo7wyIZcRYBHpb0uACVISlLbT6j Cancel-Lock: sha1:N2JmM2VjM2E5MjY4MzM2ZWE5NDQzMjllODRhNjVjNmNmYjM1NjExMg== sha1:qWztmrt8CH7EqRUvV/f1d8WDABo= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:186004 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:80144 Archived-At: Mirko 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