FMDiff™   

FileMaker Business Alliance
View Jürgen Geßwein's profile on LinkedIn

This site is W3C compliant:
Valid XHTML - Valid CSS
Last modified February 24 2016, 21:20:13 CET.

Recursive Custom Functions: Number conversion

How to process a string of characters?

Haven't you wished for a construct in a custom function in FileMaker® Pro that acts like a loop script? It can be done by letting a Custom Function call itself! Here are two useful custom functions.

Convert a HEX string to a decimal number:

Function Name and Parameters:
hex2dec ( hex )

Function Definition:
Let ( [
   h = "0123456789ABCDEF" ;
   hex = Filter ( Upper ( hex ) ; h ) ;
   d = Position ( h ; Left ( hex ; 1 ) ; 1 ; 1 ) - 1 ;
   s = Length ( hex ) - 1 
] ; 
   GetAsNumber (
   d * ( 16 ^ s ) + Case ( s > 0 ; hex2dec ( Right ( hex ; s ) ) )
   )
)

Call:
x = hex2dec ( "D00001" );

Result returned (number):
13631489

h is set to a string with allowed hex digits.
The string is converted to upper case and illegal characters are filtered out.
The leftmost character is converted to a number between 0 and 16.
The length of the whole string - 1 is calculated to determine the significance of the first digit - and whether other characters may follow.

Finally the decimal value of the leftmost digit is calculated and if there are digits left over, the Custom Function calls itself with the remaining digits which are then added to the result.

Convert a decimal number into a HEX string:

Function Name and Parameters:
dec2hex ( dec )

Function Definition:
Let ( [
   h = "0123456789ABCDEF" ;
   r  = Mod ( dec ; 16 ) ;
   d = Int ( dec / 16 ) 
] ; 
   Case ( d ; dec2hex ( d ) ) & Middle ( h ; r + 1 ; 1 )
)

Call:
x = dec2hex ( 13631489 );

Result returned (text):
D00001

h is set to a string with the hex digits.
r is set to the number modulo 16 to calculate the rightmost digit.
d is set to the remaining value.

Finally if a value remains, the Custom Function calls itself with the remaining value which is prepended to the result. The rightmost hexdigit is calculated (if any) and appended.

Have fun!

Suggestions, opinions, experience reports and other hints are always welcome via our Contacts page.


Examples are provided "AS IS" without warranties of any kind. Use at your own risk.

© 2005 - 2015 Winfried Huslik †. © 2024 Jürgen Geßwein. All Rights Reserved. FMDiff and FMVis are trademarks of Jürgen Geßwein, Augsburg, Germany. FileMaker is a trademark of FileMaker Inc., Santa Clara, CA, USA. Other trademarks mentioned are property of their respective owners. This web site has not been authorised, sponsored, or otherwise approved by FileMaker, Inc.