Brian Dunning's FileMaker Custom Functions

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   Debi Fuchs - Show more from this author
Aptworks Consulting
http://www.aptworks.com

Share on Facebook Share on Twitter

  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

 

Comments

Rico Meier   Rico Meier, MeiSol AG
Jul 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   Rico Meier, MeiSol AG
Jul 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   Debi Fuchs, Aptworks Consulting
Jul 22, 2019
Thanks, Rico, for finding that bug. I've edited the function and given you credit within it.
 
BT   BT
Jul 8, 2020
Is there a way to increase this 10bilion limit?
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Jul 8, 2020
>>Is there a way to increase this 10bilion limit?
Possibly...using the SetPrecision function in the right places.
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: