From: Johan Kullstam
Subject: Need help understanding format
Date: 
Message-ID: <m37kjtmc89.fsf@sysengr.res.ray.com>
I am trying to use format to print an integer as a binary.  Using ~B
or ~4B as format specifier, things go well.  However, I'd like to fill
with 0 rather than a space.

I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.

(format t "~&~4,0B~%" 5)

but i get

*** - WRITE-CHAR: argument 0 is not a character

i want

=> 0101

in clisp and a similar error in cmucl.

Turning to the hyperspec
<URL:http://www.xanalys.com/software_tools/reference/HyperSpec/Body/22_cba.htm>
I see the example
(format nil "~19,0,' ,4:B" 3333) =>  "0000 1101 0000 0101"

but in both clisp and cmucl i get an error about 0 not being a
character.  What exactly constitutes a "padchar" and how do I specify
0 as such?

I looked up character
<URL:http://www.xanalys.com/software_tools/reference/HyperSpec/Body/13_add.htm>
and see that 0 is a numeric character.  Does it need some kind of
special quoting for this context?  The example doesn't have any but
neither does it seem to work.

I am probably missing something trivial but vital.  Any help would be
appreciated.

-- 
Johan KULLSTAM <··········@attbi.com> sysengr

From: Sam Steingold
Subject: Re: Need help understanding format
Date: 
Message-ID: <sa0u1mxtc3y.fsf@glip.premonitia.com>
> * In message <··············@sysengr.res.ray.com>
> * On the subject of "Need help understanding format"
> * Sent on 18 Jul 2002 08:50:30 -0400
> * Honorable Johan Kullstam <··········@attbi.com> writes:
>
> I am trying to use format to print an integer as a binary.  Using ~B
> or ~4B as format specifier, things go well.  However, I'd like to fill
> with 0 rather than a space.
> 
> I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.
> 
> (format t "~&~4,0B~%" 5)
> 
> but i get
> 
> *** - WRITE-CHAR: argument 0 is not a character
> 
> i want
> 
> => 0101

(format t "~&~4,'0B~%" 5)
0101

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat7.2 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
Good programmers treat Microsoft products as damage and route around it.
From: Hannah Schroeter
Subject: Re: Need help understanding format
Date: 
Message-ID: <ah6fl5$nmt$1@c3po.schlund.de>
Hello!

Johan Kullstam  <··········@attbi.com> wrote:

>I am trying to use format to print an integer as a binary.  Using ~B
>or ~4B as format specifier, things go well.  However, I'd like to fill
>with 0 rather than a space.

>I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.

>(format t "~&~4,0B~%" 5)

The principle format of format directives is:
Tilde
arguments
modifiers (: or @)
format specifier

The arguments are separated by commas and each argument is either
a number, or a character *preceded by an apostrophe*.

I.e.

* (format t "~&~4,'0B~%" 5)

0101
NIL

works.

>but i get

>*** - WRITE-CHAR: argument 0 is not a character

You gave 0 without an apostroph which is a number.

The form of the ~B directive is:

~mincol,padchar,commachar,comma-intervalB

and padchar should be a character parameter, i.e. preceded by an apostroph.

>i want

>=> 0101

>in clisp and a similar error in cmucl.

And it works after the correction.

>Turning to the hyperspec
><URL:http://www.xanalys.com/software_tools/reference/HyperSpec/Body/22_cba.htm>
>I see the example
>(format nil "~19,0,' ,4:B" 3333) =>  "0000 1101 0000 0101"

After the obvious correction, this gives:

* (format nil "~19,'0,' ,4:b" 3333)

"000001101 0000 0101"

in CMUCL.

>[...]

Kind regards,

Hannah.
From: Marco Antoniotti
Subject: Re: Need help understanding format
Date: 
Message-ID: <y6c65zdqewv.fsf@octagon.mrl.nyu.edu>
······@schlund.de (Hannah Schroeter) writes:

> Hello!
> 
> Johan Kullstam  <··········@attbi.com> wrote:
> 
> >I am trying to use format to print an integer as a binary.  Using ~B
> >or ~4B as format specifier, things go well.  However, I'd like to fill
> >with 0 rather than a space.
> 
> >I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.
> 
> >(format t "~&~4,0B~%" 5)
> 
> The principle format of format directives is:
> Tilde
> arguments
> modifiers (: or @)
> format specifier
> 
> The arguments are separated by commas and each argument is either
> a number, or a character *preceded by an apostrophe*.
> 
> I.e.
> 
> * (format t "~&~4,'0B~%" 5)
> 
> 0101
> NIL
> 
> works.

Even more fun! Try

    (format t "#~4,VB~%" #\x 5)

which shows that 5 = 257 :)

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Petter Gustad
Subject: Re: Need help understanding format
Date: 
Message-ID: <87ptxldv9o.fsf@filestore.home.gustad.com>
Johan Kullstam <··········@attbi.com> writes:

> I am trying to use format to print an integer as a binary.  Using ~B
> or ~4B as format specifier, things go well.  However, I'd like to fill
> with 0 rather than a space.
> 
> I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.
> 
> (format t "~&~4,0B~%" 5)

(format t "~4,'0B~%" 5)


Petter
-- 
________________________________________________________________________
Petter Gustad   8'h2B | (~8'h2B) - Hamlet in Verilog   http://gustad.com
From: Pierpaolo BERNARDI
Subject: Re: Need help understanding format
Date: 
Message-ID: <3NzZ8.80416$Jj7.2073325@news1.tin.it>
"Johan Kullstam" <··········@attbi.com> ha scritto nel messaggio ···················@sysengr.res.ray.com...
>
> I am trying to use format to print an integer as a binary.  Using ~B
> or ~4B as format specifier, things go well.  However, I'd like to fill
> with 0 rather than a space.
>
> I tried in both clisp 2.28 on x86 linux and cmucl 18a on hp-ux.
>
> (format t "~&~4,0B~%" 5)

Should be: (format t "~&~4,'0B~%" 5)

See CLHS 22.3

P.
From: Christian Lynbech
Subject: Re: Need help understanding format
Date: 
Message-ID: <ofvg7dtakp.fsf@situla.ted.dk.eu.ericsson.se>
>>>>> "Johan" == Johan Kullstam <··········@attbi.com> writes:

Johan> Turning to the hyperspec
Johan> <URL:http://www.xanalys.com/software_tools/reference/HyperSpec/Body/22_cba.htm>
Johan> I see the example
Johan> (format nil "~19,0,' ,4:B" 3333) =>  "0000 1101 0000 0101"

I was utterly confused by this as well, but there simply is a bug in
the example in the hyperspec.


------------------------+-----------------------------------------------------
Christian Lynbech       | Ericsson Telebit, Skanderborgvej 232, DK-8260 Viby J
Phone: +45 8938 5244    | email: ·················@ted.ericsson.se
Fax:   +45 8938 5101    | web:   www.ericsson.com
------------------------+-----------------------------------------------------
Hit the philistines three times over the head with the Elisp reference manual.
                                        - ·······@hal.com (Michael A. Petonic)