Subject: ... and I thought I knew the quoting rules
From: Andreas Leitgeb
Date: 11/9/2007 9:29:59 AM
Helmut Giese <hgiese@ratiosoft.com> wrote:
> set cks 0
> set hex FF
> set cks [format %x [expr 0x$cks ^ 0x$hex]]
> puts "cks: [format %02X 0x$cks]"
While there are different ways to use hexnums, which are longer to type
(involving [scan ... %x]) but probably still more efficient, this is
one of the situations, where expr without braces yields the effect you
want, and with braces it wouldn't.
Anyway, the result for me shows as: "cks: FF", which seems 100%
correct to me.
So either you'd have to tell us, what other result you see, or what
makes you think, that FF was a wrong result in your opinion.
"time {expr {[scan $cks %x] ^ [scan $hex %x]}} 100000" takes less than
3/4 of the time that "time {expr 0x$cks ^ 0x$hex} 100000" takes.
If any of these numbers is constant (in a loop), then obtaining its
integervalue just once would surely again accellerate it.
Subject: ... and I thought I knew the quoting rules
From: Tom Conner
Date: 11/9/2007 7:42:58 AM
"Helmut Giese" <hgiese@ratiosoft.com> wrote in message
news:dh48j3pku389v5mgiu3dbahh3nbucoom17@4ax.com...
> Hello out there,
> I stumbled over something weird which I need some help to understand.
> I am building a checksum by XORing a sequence of bytes like so
> ---
> set cks 0
> set hex FF
> set cks [format %x [expr 0x$cks ^ 0x$hex]]
> puts "cks: [format %02X 0x$cks]"
> set hex F0
> set cks [format %x [expr {0x$cks ^ 0x$hex}]]
> puts "cks: [format %02X 0x$cks]"
> ---
>
> Anybody looking at the third line will immediately say "you should
> always brace the argument(s) to [expr]" - but when I do (in the next
> to last line) I get the error
> syntax error in expression "0x$cks ^ 0x$hex": extra tokens at end
> of expression
>
I had the exact same issue this week, both XORing and with comparisons (<=
>=). Very irritating. I ended up prepending 0x onto the var before the
expr command.
Subject: ... and I thought I knew the quoting rules
From: Glenn Jackman
Date: 11/9/2007 4:15:32 PM
At 2007-11-09 05:54AM, "Tobias Hippler" wrote:
> another way would be using double quotes around the operators inside the
> curly braces:
>
> set hex F0
> set cks [format %x [expr {"0x$cks" ^ "0x$hex"}]]
> puts "cks: [format %02X 0x$cks]"
To be precise (I love to nitpick), you mean:
double quotes around the _operands_
The operator is ^
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Subject: ... and I thought I knew the quoting rules
From: Andreas Leitgeb
Date: 11/12/2007 1:30:35 PM
Helmut Giese <hgiese@ratiosoft.com> wrote:
> foreach hex $hexLst {
> set cks [format %x [expr 0x$cks ^ 0x$hex]]
> }
I don't see the reason, why cks is thrown forth and back to hexadecimal:
set cks 0; foreach hex $hexLst { set cks [expr {$cks ^ [scan $hex %x]}] }
> lappend hexLst $cks
here convert it to hex just once:
lappend hexLst [format %x $cks]
> BTW now the spec has changed: The checksum is now a CRC - oh well,
What way ever it is calculated, there is no need to convert the
interims-results to and from hexadecimal at each iteration.
|