November 19, 200421 yr I need to convert base 10 numbers to base 2 numbers and additionally add leading zeros to fill out the converted number to 29 places. My base 10 numbers are a consequetive range given to me. For instance, I may need to begin with 10,000,000 and finish at 10,003,000. The binary conversion of that range is 24 places, so I need to add 5 leading zeros the result. I am using the binary converted number outside FMPro. I'm guessing this isn't hard for a left brainer. Thanks!
November 19, 200421 yr This would be fairly easy in version 7 with a custom function; not sure how to do it in 5. In 7, one would divide the base 10 value by 2, concatenate the remainder to the left of a global (null at the beginning), divide the quotient by 2, repeat (recurse) until the quotient is 0. The global would then represent the binary equivalent of the base 10 number. I checked Excel 2003 and Quatro Pro 12--both cannot handle such large decimal numbers. If this were my project I would use Maple 9.5: the command is convert(10000000,binary) = 100110001001011010000000, etc. One could easily loop through all your values and then export the results to disk, which then could be imported back into FileMaker.
November 19, 200421 yr Create two global number fields, gPower and gTemp. If your decimal field is dec and your binary field is bin, then the following script will work. Freeze Window Set Field [gPower, 0] Set Field [gTemp, dec] Loop Exit Loop If [int(dec / (2 ^ (gPower + 1))) = 0] Set Field [gPower, gPower + 1] End Loop Set Field [bin, Substitute( 10 ^ (28 - gPower ), 1, "" )] Loop Set Field [bin, bin & NumToText(Int(gTemp / (2 ^ gPower)) > 0)] Set Field [gTemp, Mod( gTemp, 2 ^ gPower )] Set Field [gPower, gPower - 1] Exit Loop If [gPower = -1] End Loop I should clarify that bin must be a text result.
November 19, 200421 yr Transpower is right about the presicion involved with numbers of that size, so we need to store result in a text field and do it with a script: Set Field [ theResult; "" ] Set Field [ theTemp; Source ] Loop Set Field [ theResult; Mod ( theTemp ; 2 ) & theResult ] Set Field [ theTemp; Int ( theTemp / 2 ) ] Exit Loop If [ theTemp = 0 ] End Loop Set Field [ theResult; Right("000000000000000000000000000000"& theResult;29) ] ...as you see is a temporay storage needed in type number which isn't a problem since it gets smaler each time the loop runs. --sd
November 19, 200421 yr I knew there was an easier way. I liked the challenge of working from left-to-right though. I would use S
November 19, 200421 yr Set Field [theResult, Substitute( 10 ^ (29 - Length(theResult)), 1, "" ) & theResult] Carefull carefull Queue take a look what happens when you try for say 3 ...a very short one, and all the sudden you have to an e+X to deal with. I thought of the very same thing ...but found the pesky +e in the string due to typecasting. --sd
November 19, 200421 yr Hey Queue, what's the issue with scientific notation in FM7? How many digits are preserved? (Sorry Ugo, I hate to push Queue ahead of you again.)
November 19, 200421 yr No problem http://www.fmforums.com/threads/showflat.php?Cat=0&Board=UBB45&Number=120498
November 20, 200421 yr And for those of you who have version 7, here is a custom function to do the job: // Dec2Bin ( decimalNumber ; binaryNumber ) (binaryNumber is initially "") // Converts decimal number to binary number // By Ronald W. Satz // 11-20-2004 Case ( decimalNumber = 0 ; theResult ; // Exit Let ( [theResult = Mod ( decimalNumber ; 2 ) & theResult ; decimalNumber = Int ( decimalNumber / 2 ) ] ; Dec2Bin (decimalNumber; theResult) ) // Recurse ) // Case In a calculated text field, call the function by Dec2Bin(decimalNumberfield,""). You could prepend whatever zeros you needed at the beginning (as the original poster wanted). Soren, you translated the algorithm (given in my original response) perfectly to FileMaker script form; this would be useful for version 5.
November 21, 200421 yr Soren, you translated the algorithm (given in my original response) perfectly to FileMaker script form; this would be useful for version 5. Hmm... baseconversion is part of the curriculum in math in highschool at my neck of the woods - I left highschool in 1982. I've been developing solutions in filemaker since 1995 which from time to time have been including baseconversion to some extend. --sd
Create an account or sign in to comment