blindbo Posted November 19, 2004 Posted November 19, 2004 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!
transpower Posted November 19, 2004 Posted November 19, 2004 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.
-Queue- Posted November 19, 2004 Posted November 19, 2004 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.
Søren Dyhr Posted November 19, 2004 Posted November 19, 2004 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
-Queue- Posted November 19, 2004 Posted November 19, 2004 I knew there was an easier way. I liked the challenge of working from left-to-right though. I would use S
blindbo Posted November 19, 2004 Author Posted November 19, 2004 Hopefully, praise and cheers are acceptable here. You guys are great!
Søren Dyhr Posted November 19, 2004 Posted November 19, 2004 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
-Queue- Posted November 19, 2004 Posted November 19, 2004 There is no issue with scientific notation in version 5, only in version 7.
Ugo DI LUCA Posted November 19, 2004 Posted November 19, 2004 It just makes me smile to see you as a Newbie with Intermediate skills S
Ender Posted November 19, 2004 Posted November 19, 2004 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.)
Ugo DI LUCA Posted November 19, 2004 Posted November 19, 2004 No problem http://www.fmforums.com/threads/showflat.php?Cat=0&Board=UBB45&Number=120498
transpower Posted November 20, 2004 Posted November 20, 2004 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.
Søren Dyhr Posted November 21, 2004 Posted November 21, 2004 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
Recommended Posts
This topic is 7646 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now