Page 1 of 1

using Baseconvert to convert decimal to binary

Posted: Wed Aug 21, 2013 12:00 am
by gstrekkie
Hi I am playing with livecode for about a week now and I am trying to figure out how I can baseconvert(fieldLen,10,2) to return a binary representation of a decimal number. the problem is that it seems to truncate the leading zeros. Optimally I would like the ability to set the numberformat to a specific bit size (i.e. 9 Bits) but I cant seem to wrap my head around doing this correctly and I am sure I am missing something. any answers or pointing to the right direction would be appreciated. TIA

Re: using Baseconvert to convert decimal to binary

Posted: Wed Aug 21, 2013 1:00 am
by Simon
Hi gstrekkie,
Welcome to the forum!

Normally I wouldn't just hand you the script except that there is a trick in it

Code: Select all

on mouseUp
   set the numberformat to "#########"
   put the baseConvert(26,10,2) into temp
   add 0 to temp -- dang the numberformat needs a caulation
   put temp
end mouseUp
that add 0... wierd.

Simon

Re: using Baseconvert to convert decimal to binary

Posted: Wed Aug 21, 2013 1:16 am
by gstrekkie
So thats why numberformat wasnt working for me. well thanks for the quick reply and the heads up, there are a number of things I need to get acclimated to apparently. I will post if it works for me and hopefully anyone else that might run into the issue. Thanks again!

Re: using Baseconvert to convert decimal to binary

Posted: Wed Aug 21, 2013 1:29 am
by FourthWorld
You might try the binaryEncode function.

Re: using Baseconvert to convert decimal to binary

Posted: Wed Aug 21, 2013 1:30 am
by gstrekkie
That Totally worked and makes it very easy to Pad some binary numbers to a specific length. Thanks Simon & Richard.....