Scripting a text to binary digits converter

-=>JB<=- sundown at pacifier.com
Tue Jan 3 12:58:21 EST 2012


When converting ascii to binary you convert each char using the
repeat script you provided below,

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<=-


On Jan 3, 2012, at 8:21 AM, Ken Ray wrote:

>> 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/	
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
> 





More information about the use-livecode mailing list