Re: question on funny behaviour of binary scan

From: Bruce Hartweg (bruce-news_at_hartweg.us)
Date: 02/11/05


Date: Fri, 11 Feb 2005 09:15:19 -0600


04642920123-0001@t-online.de wrote:
> Hi Tclers,
> ,
> i've run into trouble using binary scan:
>
> % binary scan "\x55" BBBBBBBB a b c d e f g h i
> 1
> % set a
> 0
> % set b
> can't read "b": no such variable
>
> % binary scan "\x55\x55" BBBBBBBB a b c d e f g h i
> 2
> % set a
> 0
> % set b
> 0
> % set c
> can't read "c": no such variable
>
> Now a is OK, b should have been 1 this look like scan
> took the next byte
>
> % binary scan "\x55\xaa" B4B* a b
> 2
> % set a
> 0101
> % set b
> 10101010
>
> a is OK but b should be 12 Bits ie: 010110101010
> and not 8 Bits?
>
> tested versions: tcl-8.4.2, tcl-8.4.6
> there seems to be no bug listed for
> the binary command at sourceforge.
>

it's not a bug, it's a feature!
seriously, that is how it is docum,ented to work - check out

http://www.tcl.tk/man/tcl8.4/TclCmd/binary.htm#M27

which has: (emphasis mine)

b
     The data is turned into a string of count binary digits in low-to-high order
     represented as a sequence of ``1'' and ``0'' characters. The data bytes are
     scanned in first to last order with the bits being taken in low-to-high order
     within each byte.
**********************************************************
***** Any extra bits in the last byte are ignored. ******
**********************************************************
     If count is *, then all of the remaining bits in string will be scanned.
     If count is omitted, then one bit will be scanned. For example,

binary scan \x07\x87\x05 b5b* var1 var2

     will return 2 with 11100 stored in var1 and 1110000110100000 stored in var2.

B
     This form is the same as b, except the bits are taken in high-to-low order
     within each byte. For example,

binary scan \x70\x87\x05 B5B* var1 var2

     will return 2 with 01110 stored in var1 and 1000011100000101 stored in var2.

Bruce



Relevant Pages

  • Re: The Generic Heroine
    ... her into trouble again and again throughout the series-- her tragic ... I had this problem with one of my characters. ... What are her passions (besides her undying loyalty to Humanity)? ... The tragic past sounds interesting. ...
    (rec.arts.sf.composition)
  • Re: 1 Year Later Creating a 14-Year DCU Timeline?
    ... > That's not the trouble. ... > but six months for OTHER of the characters. ... Say Robin is in an adventure that takes ... the creative teams on different titles. ...
    (rec.arts.comics.dc.universe)
  • Re: >>max. length in /etc/hosts<<
    ... you are asking for trouble if you get anywhere near ... Limits of both 14 and 31 characters were common until ... going over a dozen characters between dots or about ... University of Cambridge Computing Service, ...
    (comp.security.unix)
  • Re: Noob File IO question
    ... trouble with File IO http://www.openwatcom.org/index.php/Download ... commented out the Command line ... When I debug this and watch the input buffer ... characters and my program thinks they are 8 bit. ...
    (comp.lang.c)
  • Re: Freecycle - again
    ... the characters appear with a slight delay (about half a ... second or so) for some reason or other. ... I wonder if there's trouble ...
    (uk.rec.sheds)