Scripting a text to binary digits converter
Ken Ray
kray at sonsothunder.com
Tue Jan 3 11:21:52 EST 2012
> 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
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: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/
More information about the use-livecode
mailing list