Problem with [binary scan] and sign in Tcl 8.4 
Author Message
 Problem with [binary scan] and sign in Tcl 8.4

I'm trying to parse MAC addresses out of binary buffers with [binary
scan] but negative signs keep creeping in:

  proc test { } {
    set msg [binary format "c c c c c c" 1 0x80 0xc2 0 0 0]
    binary scan $msg "c6" dst
    puts "dst:$dst"
    puts -nonewline "msg from"
    set sep " "
    foreach e $dst {
        puts -nonewline "$sep[format %02x $e]"
        set sep ":"
    }
    puts ""
  }

produces:

  dst:1 -128 -62 0 0 0
  msg from 01:ffffff80:ffffffc2:00:00:00

I can't find an option to tell [binary scan] to treat the bytes as
unsigned.  Am I missing something?



Mon, 12 Nov 2007 00:37:44 GMT  
 Problem with [binary scan] and sign in Tcl 8.4
Quote:

> I'm trying to parse MAC addresses out of binary buffers with [binary
> scan] but negative signs keep creeping in:

>   proc test { } {
>     set msg [binary format "c c c c c c" 1 0x80 0xc2 0 0 0]
>     binary scan $msg "c6" dst
>     puts "dst:$dst"
>     puts -nonewline "msg from"
>     set sep " "
>     foreach e $dst {
>    puts -nonewline "$sep[format %02x $e]"
>    set sep ":"
>     }
>     puts ""
>   }

> produces:

>   dst:1 -128 -62 0 0 0
>   msg from 01:ffffff80:ffffffc2:00:00:00

> I can't find an option to tell [binary scan] to treat the bytes as
> unsigned.  Am I missing something?

There is no option for unsigned..., read the man page, there is an
example how to mask with & to get unsigned values, but this works only
up to 63 bit values (as Tcl has no larger internal type).

Michael



Mon, 12 Nov 2007 00:49:51 GMT  
 Problem with [binary scan] and sign in Tcl 8.4

Quote:


> > I'm trying to parse MAC addresses out of binary buffers with [binary
> > scan] but negative signs keep creeping in:

> > ...

> > I can't find an option to tell [binary scan] to treat the bytes as
> > unsigned.  Am I missing something?

> There is no option for unsigned..., read the man page, there is an
> example how to mask with & to get unsigned values, but this works only
> up to 63 bit values (as Tcl has no larger internal type).

Ahh, just the thing:

  proc parse_ether { msg aName } {
    upvar $aName a

    binary scan $msg "c6 c6 S a*" dst src a(tl) a(data)
    foreach f {dst src} {
        set a($f) {}
        foreach e [set $f] {
            lappend a($f) [expr {$e & 0xff}]
        }
    }
  }

Thanks.



Mon, 12 Nov 2007 01:20:06 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Wierd Signed Versus Unsigned Behavior in TCL 8.4.*

2. BLT binaries for TCL 8.4 on Solaris 9

3. BLT binaries for TCL 8.4 on Solaris 2.8

4. tkogl - tcl/tk 8.4 precompiled binary DLLs

5. need blt binaries for tcl/tk 8.4 ( and threads as well)

6. problem about tcl 8.3 and 8.4

7. Problem compiling TCL 8.4 on HP-UX 11

8. Speed problem with Tcl 8.4 and file operations

9. Serial Read Problem in TCL 8.4

10. Tcl 8.4: package require problem

11. togl binary as loadable dll for 8.4 (for win)

12. Install tcl 8.4 error, Can't find a usable init.tcl in the following directories

 

 
Powered by phpBB® Forum Software