Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: cf5dd6d9304f6ab6983c7c8e61b245e06d4b2ceb
Ticket: 1824688fffffffffffffffffffffffffffffffff
format should have a %b for converting integer to binary
User & Date: mboffey2 on 2008-10-23 06:19:01
Changes

  1. icomment:
    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;
    
    
    
  2. login: "mboffey2"
  3. mimetype: "text/plain"
  4. username: "mboffey2"