Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binary representation for [format] and [scan] ?

117 views
Skip to first unread message

Jonathan Bromley

unread,
Nov 30, 2008, 11:48:05 AM11/30/08
to
Rummaging around the Wiki for ideas on how to get
ordinary Tcl-style numeric values re-formatted as
binary (in other words, a string like "1001111101")
I found a lot of fun, and several appearances of the
[binary scan [binary format ...] ...] scheme that
I've been using myself for some time. But nowhere
do I find any suggestion that Tcl might add a %b
(binary) formatter to the repertoire of [format]
and [scan]. Given the pervasiveness of Tcl in
the electronics design business, wouldn't that
be a good idea?

For the sake of clarity, let me emphasise that I'm
not talking about binary in the way that [binary]
does; I'm interested in dealing with binary numbers
at the script level in the same way that I can
currently manipulate octal and hex. What I seek is:

(1) a %b numeric formatter for [format] and [scan]:
[format %b 33] should yield "100001"
[format %08b 33] should yield "00100001"
[scan 1001 %b] should yield "9"

(2) binary numbers as literal numeric strings:
[expr 3 + 0b101] should yield "8"

Anyone else interested? Or is it just me?
Messing around with [binary] is just painful,
especially when dealing with arbitrary-width
binary number strings, and the [binary] man page
is uncharacteristically obscure by the usual high
standards of Tcl documentation.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

Donal K. Fellows

unread,
Nov 30, 2008, 1:09:53 PM11/30/08
to
Jonathan Bromley wrote:
> (1) a %b numeric formatter for [format] and [scan]:
>   [format %b 33] should yield "100001"
>   [format %08b 33] should yield "00100001"
>   [scan 1001 %b] should yield "9"

Reasonable idea, but won't make 8.6. Do submit a FRQ (or, better yet,
a TIP and patch) on the topic so it doesn't get forgotten.

> (2) binary numbers as literal numeric strings:
>   [expr 3 + 0b101]  should yield "8"

Are you using 8.5? If so, try *exactly* what you've written. :-D

Donal.

Alexandre Ferrieux

unread,
Nov 30, 2008, 7:56:47 PM11/30/08
to
On Nov 30, 7:09 pm, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:

Just added patch 2368084 which does exactly that.

https://sourceforge.net/tracker/index.php?func=detail&aid=2368084&group_id=10894&atid=310894

Repeating the comment there:
The attached patch adds a %b specifier meaning 'binary' to the
[format] and
[scan] commands. It is thus continuing in the direction pioneered by
the
recent addition of "0b" in [expr]'s number parsing.

The specifier of course also works with "#" (adding "0b" if nonzero)
and
wide- and bignum modifiers (%lb, %llb).

In addition, the patch fixes the following bug in [scan]:

% scan 0b101 %i x
1
% puts $x
0

The patch includes proper tests and documentation update. Does it
really
need a TIP ?

-Alex

Jonathan Bromley

unread,
Dec 1, 2008, 5:37:27 AM12/1/08
to
On Sun, 30 Nov 2008 16:56:47 -0800 (PST), Alexandre Ferrieux wrote:

>> Jonathan Bromley wrote:
>> > (1) a %b numeric formatter for [format] and [scan]:
>> >   [format %b 33] should yield "100001"
>> >   [format %08b 33] should yield "00100001"
>> >   [scan 1001 %b] should yield "9"
>

Ummmm, wow, that was quick...
And your neat implementation probably shows why
I should not have been trusted to do the patch :-)

thanks

0 new messages