What is the simplest way of doing this in livecode? Could something like
this also be done for photos? Or better I suppose for photos would be
conversion to ascii text.
Thanks in advance for ideas and pointers.
_______________________________________________
use-livecode mailing list
use-li...@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Put the whole thing in a button to test how it works and
then rewrite the code without using ask and answer.
-=>JB<=-
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASCII-Bin Convert
Convert between ASCII-Binary
written by John Balgenorth - 7/31/08
on mouseUp
answer "convert" with "ASCII-Bin" or "Bin-ASCII"
if it = "ASCII-Bin" then convABin
if it = "Bin-ASCII" then convBinA
end mouseUp
on convABin
ask "convert ASCII to Binary"
if it = empty or the result = "cancel" then exit to top
put it into charConv
put chartonum(charConv) into theNum
put baseconvert(theNum,10,2) --into fld id 1010
end convABin
on convBinA
ask "convert Binary to ASCII"
if it = empty or the result = "cancel" then exit to top
put it into theNum
put baseconvert(theNum,2,10) into charConv
put numtochar(charConv) --into fld id 1010
end convBinA
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
You can easily use the *binaryDecode()* and *binaryEncode()* functions ...
... see the "dictionary"
Guglielmo
--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripting-a-text-to-binary-digits-converter-tp4254072p4254238.html
Sent from the Revolution - User mailing list archive at Nabble.com.
-=>JB<=-
This appears to work for my purposes and is pretty speedy. See anything
that might break?
---------------------------------------
function convertTextToBinary varText
repeat with n = 1 to the number of chars of varText
put chartonum(char n of varText) into theNum
if theNum >=64 and theNum <= 127 then
put "0"&baseConvert(theNum,10,2) after tConverted
else if theNum <="64" then
put "00"&baseConvert(theNum,10,2) after tConverted
else
put baseConvert(theNum,10,2) after tConverted
end if
end repeat
return tConverted
end convertTextToBinary
-=>JB=-
Well it gives some peculiar results because I clearly don't know much
about the encoding process in terms of passing the right params.
I rewrote this to be more general. Give it a try if you like and let me
know. It will handle carriage returns and such properly.
Seems speedy enough for me but again, I am sure it can be improved upon.
Thanks for your help, most generous of you.
----------------------
function convertTextToBinary varText
repeat with n = 1 to the number of chars of varText
put chartonum(char n of varText) into theNum
put baseConvert(theNum,10,2) into tBaseConverted
if length(tBaseConverted) <8 then
repeat until length(tBaseConverted)= 8
put "0" before tBaseConverted
end repeat
end if
put tBaseConverted after tConverted
Just for fun I tried with a small script you function :
put convertTextToBinary(theText) into field "outField"
... and the binaryDecode :
put binaryDecode("B*", theText, binText) into numItems
put binText into field "outFieldBin"
both inserted in two separate "repeat" to repeat the same statements 200
times, storing the millisecond to execute.
Whit your function, on my Win 7 64 bit system, I have between 60 and 70
milliseconds, with the binaryDecode() + the put statemet (/... two separate
statements/) I have between 20 and 30 milliseconds :)
So .. the Livecode binaryDecode + the put statement is minimum *TWO* time
faster than calling your function ;)
Guglielmo
--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripting-a-text-to-binary-digits-converter-tp4254413p4254500.html
Sent from the Revolution - User mailing list archive at Nabble.com.
_______________________________________________
Guglielmo
> put baseConvert(theNum,10,2) into tBaseConverted
> if length(tBaseConverted) <8 then
> repeat until length(tBaseConverted)= 8
> put "0" before tBaseConverted
> end repeat
> end if
Alternative technique is to...
put char -8 to -1 of ("00000000" & tBaseConverted ) into tBaseConverted
Jim Ault
Thanks so much. I wonder how much faster that is? And Guglielmo, I
appreciate you taking a crack at this. I don't get how to do the encode
using your approach at all since I am not familiar with the params for
the BinaryEncode function.
Okay, this is the final version of the script which does seem
considerably faster than where I started.
Thanks to all. Hope this was a useful walk-through.
Any other suggestions on ways to improve?
---------------------------------------------
function convertTextToBinary varText
repeat with n = 1 to the number of chars of varText
put chartonum(char n of varText) into theNum
put baseConvert(theNum,10,2) into tBaseConverted
put char -8 to -1 of ("00000000" & tBaseConverted ) into
tBaseConverted
> Jim,
>
> Thanks so much. I wonder how much faster that is? And Guglielmo, I appreciate you taking a crack at this. I don't get how to do the encode using your approach at all since I am not familiar with the params for the BinaryEncode function.
>
You could do your own bench-marking by
---------------------
put "135898" into varText
put 100 * 1000 into loopCnt
repeat loopCnt times
put varText & cr after veryLongList
end repeat
put the ticks into startTicks
repeat for each line LNN in veryLongList
put convertTextToBinary(varText) into tConverted
end repeat
put the ticks - startTicks into elapsedTicks
put elapsedTicks div 60 into elapsedSeconds
get elapsedTicks && " tick count"
get IT & cr & elapsedSeconds && " tick count"
get IT & cr & loopCnt/elapsedTicks && " conversions per tick"
put IT into msg
------------------------
Jim Ault
I got roughly a 6% speed improvement using 'repeat for each char tChar' instead
of repeat with n = 1..."
My test sample was 1000 chars.
function convertTextToBinary varText
--repeat with n = 1 to the number of chars of varText
repeat for each char tChar in varText
--put chartonum(char n of varText) into theNum
put chartonum(tChar) into theNum
put baseConvert(theNum,10,2) into tBaseConverted
put char -8 to -1 of ("00000000" & tBaseConverted ) into tBaseConverted
put tBaseConverted after tConverted
end repeat
return tConverted
end convertTextToBinary
Phil Davis
--
Phil Davis
PDS Labs
Professional Software Development
http://pdslabs.net
Another thing you could do which *might* speed things up is to use numberFormat instead of parsing strings:
function convertTextToBinary varText
set the numberFormat to "00000000"
--repeat with n = 1 to the number of chars of varText
repeat for each char tChar in varText
--put chartonum(char n of varText) into theNum
put chartonum(tChar) into theNum
put (baseConvert(theNum,10,2)+0) into tBaseConverted -- the "+0" forces it to use the number format
put tBaseConverted after tConverted
end repeat
return tConverted
end convertTextToBinary
You can also collapse a lot of the code (although it's less readable):
function convertTextToBinary varText
set the numberFormat to "00000000"
repeat for each char tChar in varText
put (baseConvert(chartonum(tChar),10,2)+0) after tConverted
end repeat
return tConverted
end convertTextToBinary
Stripping a few lines may also increase speed - don't know but just a thought...
Ken Ray
Sons of Thunder Software, Inc.
Email: kr...@sonsothunder.com
Web Site: http://www.sonsothunder.com/
repeat for each char tChar in varText
But when you want to convert the binary to ascii you need to get
eight char then convert then repeat eight chars.
How would you write the fastest repeat structure to get eight chars
then repeat until you used them all?
-=>JB<=-
Function convertBinaryToText varText
repeat with x = 1 to the len of varText-8 step 8
put numtochar(BaseConvert(char x to (x+7) of varText,2,10)) after tConverted
end repeat
return tConverted
end convertBinaryToText
-=>JB<=-
Oddly your terse script ran a bit longer than the "final" one the group
cobbled together by community heavy lifting. Not by much though. 1000
repeats on both showed yours was about 13 to 20 ticks slower.
Not at all what I would have expected either.
Your final version of the script ended up winning the speed race. See my
note to Ken.
Thanks to everyone for pitching in. That was fun. Forced me to learn a
bit about baseConvert and binaryEncode/Decode