Harsh lessons in binaryDecode
Alex Tweedly
alex at tweedly.net
Sat May 27 08:33:44 EDT 2006
I was trying to use binaryDecode to get some data out of a string; the
format is simple
- 4 digit number
- a space
- 4 digit number
- a space
- 12 digit number
However, the numbers may be completely blank (otherwise I'd just have
used word 1, word 2 and word 3 :-)
So get some sample data ready ...
put "1234 5678 123456789012 and some more" into lV
Rather than
put char 1 to 4 of V into tVal1
put char 6 to 9 of V into tVal2
put char 11 to 22 of V into tVal3
I decided to have a go at using binaryDecode (thanks to Jim Ault for
suggesting this), because it would be a single statement and potentially
faster (the real case has more variables and decoding to do)
get binaryDecode("a4xa4xa12", lV, tVal1, tVal2, tVal3)
i.e. put 4 chars into the first variable, skip one char, 4 chars into
next var, skip, 12 chars into next var.
I heeded the warning about the fact that the variables must exist, so
added local declarations.
Unfortunately, executing this code makes Rev crash immediately - no
error, no warning, just "Boom" - gone.
It turns out (I think) that although the binaryDecode format of "x" is
described as
> The formatsList consists of one or more dataTypes, each followed
> optionally by an amount. A dataType is one of the following single
> letters:
>
> x: skip next amount bytes of data
> a: convert next amount bytes of data to characters
etc.
"skip" doesn't mean skip - it means skip and use up one variable from
the variable list !!
Therefore, there were too few variables in the variable list. A crash
out of Rev seems a bit harsh even for too few variables, but I can live
with that.
A quick scan in Bugzilla didn't show anything like this. So
- should I BZ it ? (or this the way it's always been and should be
forever :-)
- BZ the docs ? (i.e. skip doesn't mean skip), since a change would
risk backwards compatibility
- BZ the crash separately ??
Here's the recipe - switch the commented out lines to see the problem ...
[ in the script of a button ]
local lV
on mouseUp
put "1234 5678 123456789012 and some more" into lV
doit
end mouseUp
on doit
local tVal1, tVal2, tVal3
local x1, x2
-- get binaryDecode("a4xa4xa12", lV, tVal1, tVal2, tVal3)
get binaryDecode("a4xa4xa12", lV, tVal1, x1, tVal2, x2, tVal3)
put it && tVal1, tVal2, tVal3 & cr after msg
end doit
Moral of the day, for those who've made it this far ...
I've spent more hours with this problem than I will ever save seconds
in run time from this efficiency :-)
--
Alex Tweedly http://www.tweedly.net
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.7.2/349 - Release Date: 26/05/2006
More information about the use-livecode
mailing list