 BaseConvert ( string ; baseFrom ; baseTo )

Convert a number (up to 10 billion) from one base to another.     Average rating: 4.4 (41 votes) Log in to vote Debi Fuchs - Show more from this author Aptworks Consulting http://www.aptworks.com
Sample input:
BaseConvert ( "F1"; 16; 2 )
Sample output:
"11110001"

Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

CUSTOM FUNCTION: BaseConvert ( string ; baseFrom ; baseTo )
//written by Debi Fuchs of Aptworks Consulting, debi@aptworks.com
//bug fix by Rico Meier, MeiSol AG

Convert a number (up to 10 billion) from one base to another.

EXAMPLES:

BaseConvert( "F1"; 16; 2 ) // --> "11110001"
BaseConvert( -5; 10; 2 ) // --> "-101"
BaseConvert( 255; 10; 16 ) // --> "FF"
BaseConvert( "-101" ; 2; 10 ) // --> "-5"
BaseConvert( -101 ; 2; 10 ) // --> -5
BaseConvert( "FF"; 16; 10 ) // --> "255"
BaseConvert( "-101" ; 16; 2 ) // --> "-100000001"
BaseConvert( "-101" ; 2; 16 ) // --> "-5"
BaseConvert( "FF"; 16; 2 ) // --> "11111111"

IMPLEMENTATION: See comments. No helper functions are required. Limit is set at 10^10 so that the math does not break down, as log calculations on large numbers can be incorrect.

LAST MODIFIED: 22-JUL-2019 by Debi Fuchs of Aptworks Consulting Rico Meier, MeiSol AGJul 1, 2019 To avoid an error by submit BaseConvert( "00"; 16; 2 ) -> 0? change this: // Return a 0 value if the string represents 0; string = 0 or string = "0"; Case( baseTo=10; 0; "0" ); to // Return a 0 value if the string represents 0; getasnumber (string) = 0 or string = "0"; Case( baseTo=10; 0; "0" ); thanks Rico Meier, MeiSol AGJul 2, 2019 Sorry, this will not working because "AB"; 16; 2 will get -> 0. change to: // Return a 0 value if the string represents 0; ( length (string) = patternCount ( string; "0") and GetAsNumber (string) = 0 ) or string = "0"; Case( baseTo=10; 0; "0" ); Debi Fuchs, Aptworks ConsultingJul 22, 2019 Thanks, Rico, for finding that bug. I've edited the function and given you credit within it.