Scripting a text to binary digits converter

Michael Doub mike at doub.com
Tue Jan 3 15:40:27 EST 2012


How's this?

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


On 2012-01-03, at 11: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