Skip to content
Tcl Source Code View Ticket
Login

View Ticket

format should have a %b for converting integer to binary

Closed / Out of Date1824688fffffffffffffffffffffffffffffffffCreated 19 years agoUpdated 17 years ago
Descriptionplain2007-11-02 14:46:56
the format function in tcl has conversion specifiers for converting to decimal, octal, and hexadecimal.

I'm requesting a conversion specifier for converting to binary.

There are times when one has a need, at least during debugging, to take a look at a variable's bit pattern to see what bits are on and off.  While one could certainly write tcl code to test each bit and output a 1 or a 0 in response, having a conversion character would allow Tcl to do the right thing. Given that Tcl supports 0bNNN for a built-in base specifier, it would seem natural to support perhaps %b to generate an appropriate string.
dkf Ticket [1824688fff]: 1 change [History diff]2007-11-02 23:06:50
dkf Ticket [1824688fff]: 1 change [History diff]2007-12-02 20:14:59
mboffey2plain2008-10-23 06:19:01
The following patch does what you are after I believe.

A slightly more involved question is should [scan] also deal with binary numbers.

a. should [scan 0b1010 %b] work as well as [scan 1010 %b]
  - hex can be optionally prefixed with 0x, octal with 0 but not 0o
b. should the %i format work - the man page seems to say that it already does -
       i         The input substring must be an integer.  The base (i.e. decimal, binary,  octal,
                 or  hexadecimal)  is  determined  in the same fashion as described in expr.  The
                 integer value is stored in the variable, truncated as required by the size modi-
                 fier value.

However, tcl8.6a2 gives -
% scan 0x1234 %i
4660
% scan 0b1010 %i
0
% scan 0o1234 %i
0
% scan 01234 %i
668
% scan 1234 %i
1234

Malc.

--- tcl8.6a2/generic/tclStringObj.c     2008-04-07 16:23:10.000000000 +0100
+++ tcl8.6a2-binary/generic/tclStringObj.c      2008-10-01 23:00:55.000000000 +0100
@@ -1942,6 +1942,7 @@
                goto errorMsg;
            }
        case 'd':
+       case 'b':
        case 'o':
        case 'x':
        case 'X': {
@@ -2009,6 +2010,9 @@
 
            if (gotHash) {
                switch (ch) {
+               case 'b':
+                   Tcl_AppendToObj(segment, "0b", 2);
+                   break;
                case 'o':
                    Tcl_AppendToObj(segment, "0", 1);
                    precision--;
@@ -2073,6 +2077,7 @@
            }
 
            case 'u':
+           case 'b':
            case 'o':
            case 'x':
            case 'X': {
@@ -2085,6 +2090,10 @@
                if (ch == 'u') {
                    base = 10;
                }
+               if (ch == 'b') {
+                   base = 2;
+                   numBits = 1;
+               }
                if (ch == 'o') {
                    base = 8;
                    numBits = 3;
mboffey2plain2008-10-23 06:22:12
The following patch does what you are after I believe.

A slightly more involved question is should [scan] also deal with binary numbers.

a. should [scan 0b1010 %b] work as well as [scan 1010 %b]
  - hex can be optionally prefixed with 0x, octal with 0 but not 0o
b. should the %i format work - the man page seems to say that it already does -
       i         The input substring must be an integer.  The base (i.e. decimal, binary,  octal,
                 or  hexadecimal)  is  determined  in the same fashion as described in expr.  The
                 integer value is stored in the variable, truncated as required by the size modi-
                 fier value.

However, tcl8.6a2 gives -
% scan 0x1234 %i
4660
% scan 0b1010 %i
0
% scan 0o1234 %i
0
% scan 01234 %i
668
% scan 1234 %i
1234

Malc.

--- tcl8.6a2/generic/tclStringObj.c     2008-04-07 16:23:10.000000000 +0100
+++ tcl8.6a2-binary/generic/tclStringObj.c      2008-10-01 23:00:55.000000000 +0100
@@ -1942,6 +1942,7 @@
                goto errorMsg;
            }
        case 'd':
+       case 'b':
        case 'o':
        case 'x':
        case 'X': {
@@ -2009,6 +2010,9 @@
 
            if (gotHash) {
                switch (ch) {
+               case 'b':
+                   Tcl_AppendToObj(segment, "0b", 2);
+                   break;
                case 'o':
                    Tcl_AppendToObj(segment, "0", 1);
                    precision--;
@@ -2073,6 +2077,7 @@
            }
 
            case 'u':
+           case 'b':
            case 'o':
            case 'x':
            case 'X': {
@@ -2085,6 +2090,10 @@
                if (ch == 'u') {
                    base = 10;
                }
+               if (ch == 'b') {
+                   base = 2;
+                   numBits = 1;
+               }
                if (ch == 'o') {
                    base = 8;
                    numBits = 3;
mboffey2plain2008-10-23 06:29:03
The following patch does what you are after I believe.

A slightly more involved question is should [scan] also deal with binary numbers.

a. should [scan 0b1010 %b] work as well as [scan 1010 %b]
  - hex can be optionally prefixed with 0x, octal with 0 but not 0o
b. should the %i format work - the man page seems to say that it already does -
       i         The input substring must be an integer.  The base (i.e. decimal, binary,  octal,
                 or  hexadecimal)  is  determined  in the same fashion as described in expr.  The
                 integer value is stored in the variable, truncated as required by the size modi-
                 fier value.

However, tcl8.6a2 gives -
% scan 0x1234 %i
4660
% scan 0b1010 %i
0
% scan 0o1234 %i
0
% scan 01234 %i
668
% scan 1234 %i
1234

Malc.

--- tcl8.6a2/generic/tclStringObj.c     2008-04-07 16:23:10.000000000 +0100
+++ tcl8.6a2-binary/generic/tclStringObj.c      2008-10-01 23:00:55.000000000 +0100
@@ -1942,6 +1942,7 @@
                goto errorMsg;
            }
        case 'd':
+       case 'b':
        case 'o':
        case 'x':
        case 'X': {
@@ -2009,6 +2010,9 @@
 
            if (gotHash) {
                switch (ch) {
+               case 'b':
+                   Tcl_AppendToObj(segment, "0b", 2);
+                   break;
                case 'o':
                    Tcl_AppendToObj(segment, "0", 1);
                    precision--;
@@ -2073,6 +2077,7 @@
            }
 
            case 'u':
+           case 'b':
            case 'o':
            case 'x':
            case 'X': {
@@ -2085,6 +2090,10 @@
                if (ch == 'u') {
                    base = 10;
                }
+               if (ch == 'b') {
+                   base = 2;
+                   numBits = 1;
+               }
                if (ch == 'o') {
                    base = 8;
                    numBits = 3;
dkfplain2009-07-13 01:31:01
allow_comments - 1

This feature is part of Tcl 8.6:
  % format %b 123
  1111011
  % scan 1111011 %b
  123
  % info patch
  8.6b1.1
Log in (anonymous is fine) to add a comment.