Reputation: 1
I have got a problem with this string comparison script:
set result [string range "0002150C0E67" 8 11 ];
if {[expr {$result != "FFFF" && $result != "0000"}]} {set result "OK byte 5&6 = 0x$result" } {set result "Fail Byte 5&6 = 0x$result" };
If I change "0002150C0E67" to "0002150C0D67" or "0002150C0F67" (or a lot of other combinations), I get an OK result.
What is wrong?
Upvotes: 0
Views: 39
Reputation: 137807
If we look at your code:
set result [string range "0002150C0E67" 8 11 ]
At this point, result
is set to the string 0E67
. That's a string that looks like a floating point number that evaluates to 0×1067, which is just zero.
Now, simplifying your conditional (the extra expr
added nothing) and reformatting to follow conventional style (in a functionally equivalent way), we get this:
if {$result != "FFFF" && $result != "0000"} {
set result "OK byte 5&6 = 0x$result"
} else {
set result "Fail Byte 5&6 = 0x$result"
}
As we can see, the second not-equals comparison is not satisfied so the "Fail" path is chosen. Far better is to parse the string like this:
scan "0002150C0E67" "%8x%4x" -> result
Then we do numeric comparisons on result and format
to produce output:
if {$result != 0xFFFF && $result != 0} {
set result [format "OK byte 5&6 = 0x%04x" $result]
} else {
set result [format "Fail Byte 5&6 = 0x%04x" $result]
}
Upvotes: 2