November 22, 200421 yr I feel like a copycat, but I'm also looking for a base conversion algorithm. In my case I want to go the other direction. I want to go from either decimal (preferred) or hex to base 36. The goal being to convert something sort of like an MD5 hash into a shorter alphanumeric string. A custom function would be nice, but a script will work. Any kind folks (or show-offs) want to take a stab at it?
November 22, 200421 yr Just a tip: Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Substitute ( Lower ( Source ); "0" ; "0000" ); "1" ; "0001" ); "2" ; "0010" ); "3" ; "0011" ); "4" ; "0100" ); "5" ; "0101" ); "6" ; "0110" ); "7" ; "0111" ); "8" ; "1000" ); "9" ; "1001" ); "a" ; "1010" ); "b" ; "1011" ); "c" ; "1100" ); "d" ; "1101" ); "e" ; "1110" ); "f" ; "1111" ) => Substitute ( Lower(Source); ["0" ; "0000"]; ["1" ; "0001"]; ["2" ; "0010"]; ["3" ; "0011"]; ["4" ; "0100"]; ["5" ; "0101"]; ["6" ; "0110"]; ["7" ; "0111"];["8" ; "1000"]; ["9" ; "1001"]; ["a" ; "1010"]; ["b" ; "1011"]; ["c" ; "1100"];["d" ; "1101"]; ["e" ; "1110"]; ["f" ; "1111"] )
November 22, 200421 yr JSubstitute ( Lower(Source); ["0" ; "0000"]; ["1" ; "0001"]; ["2" ; "0010"]; ["3" ; "0011"]; ["4" ; "0100"]; ["5" ; "0101"]; ["6" ; "0110"]; ["7" ; "0111"];["8" ; "1000"]; ["9" ; "1001"]; ["a" ; "1010"]; ["b" ; "1011"]; ["c" ; "1100"];["d" ; "1101"]; ["e" ; "1110"]; ["f" ; "1111"] ) Excellent, havn't quite embraced the new syntax ...I recieved my copy of 7.0 only 14 days ago
November 23, 200421 yr Author OK, this works great! I was able to expand the algorithm out to handle values up to 36^15 without having to extend the repeating fields past 128, so there's not much of a performance drop, and it should be able to hold an MD5 with no problem. Now I just need to understand it. This new substitute syntax -Queue- provided is interesting, and I can make sense of it. But then there's this... Let ( aVar=Length ( Extend ( AsBinary ) ) - Get(CalculationRepetitionNumber); Choose ( Sign ( aVar); aVar; aVar) I see what it's doing, but how does it do it? It's clear I need to bone up on some of the new functions in FM7.
November 23, 200421 yr The trouble is that you need to know how long the binary number is to make the correct 2^x 'es. say you have the binary 1101 then is it in reading direction 1*2^3 + 1*2^2 + 0*2^1+1*2^0...But if we just look at this: Length ( Extend ( AsBinary ) ) - Get(CalculationRepetitionNumber) Will it our case with 1101 make the repeating fields fill like this 3,2,1,0,-1,-2-3 etc, but we'll only need the first four of them. Choose( does accept all positive integers and 0, as pointer to which part of the calc' to use, this filters all the negative away because the pointer points undefined below 0. The function Sign( have only 3 states -1,0,1 that comes in handy here!!! Choose ( Sign ( aVar); aVar; aVar) makes the repetitions fill with only the positive, so we in the next field can exploit that feature Filemaker Inc have called "Do not evaluate if all referenced fields are empty" . If I hadn't done it this way would I have had to postfix a ton of "0" below the decimal point. As soon as you understand it will you know how to go directly from Hex to Base36 sans the binary which is overkill in this case. Where it could have made some sence is Base32.... (I actually misread first time around) --sd
November 23, 200421 yr Since you are using Dev 7, a Custom Function would be more efficient than calculated reps. See here for my decimal to base N converter CF. It might give you some ideas how to accomplish this. Sadly, I don't have time to delve into it right now.
Create an account or sign in to comment