I’m updating my FileMaker Unicode database database to reflect the changes in the recent versions of Unicode. As part of the database, I like to have the decimal version of the code point handy as well as the actual hexadecimal version (it’s good for debugging purposes).
Now the default version does not appear to have to hex to decimal conversion built in (not even in FileMaker 10), so here’s my (updated) solution.
- In the main table corresponding to the list of code points, I created a field for the Hexadecimal Unicode code point value. I’ll call this HexValue for now. It must be a Text field. You can create a Decimal field (Calculated), but you won’t be able to fill in the formula yet.
- Then I created a second table to store the correspondence between a hex digit (0-F) and its decimal value (0-15). The HexValuefield is Text, but the DecValue field is a Number. See the sample table below (some values skipped).
HexValue (Text) DecValue (Number) 0 0 1 1 2 2 3 3 4…9 (1 row each) 4…9 A 10 B 11 C…E (1 row each) 12…14 F 15
To do all the conversions, you need to extract the text value of each position in the code point. So, I created fields corresponding to the value for each place in the hex code point as shown in the list below. I’ll explain the formulas below.
Note: In case you’re wondering, the name of the places are semi-inspired by Roman numerals and algebra.
- Rightmost digit Units (n) : nhex = Right(HexValue;1)
- Penultimate digit (t) : thex = Left(Right(UnicodeHex;2);1)
- Antepenultimate digit (c) : chex = Left(Right(UnicodeHex;3);1)
- 4th from right (m): mhex =Left(Right(UnicodeHex;4);1)
- 5th from right (d): dhex =If(Length (UnicodeHex)>4;Left(Right(UnicodeHex;5);1);”0″)
- 6th from right (x): xhex = If(Length (UnicodeHex)>5;Left(Right(UnicodeHex;6);1);”0″)
The challenge for modern Unicode is that code points now come in variable lengths (4-6), so if you count from the left you can’t always know you are the appropriate digit. That means you have to count from the right, but there’s no simple formula for picking the 2nd digit from the right. My solution is to take a rightmost chunk then count in from the left. So to get the 3rd hex digit from the left, I take the right most 3 digits, then find the leftmost digit in that chunk (hence the embedded left(right) formulas).
I also have to check to see if the length is greater than 4. When the length is 4, some digits are filled in with the value 0, otherwise you do a string extraction. Hence the formulas for dhex and xhex use conditional logic. Hopefully though, if Unicode adds more digits, these formulas will continue to work (unlike my original attempt which only assumed 4 digits in the code point.
- To convert each extracted digit to its decimal version. I need to set up some Relationships between tables so that each extracted digit can look up the decimal equivalent. For each of the intermediate digit fields above, I created a link to an instance of the Hexadecimal Lookup table (there are 4 instances total). It’s important to make sure each instance has a name you can remember later; mine mention which digit I am working on. See the Relationships diagram below.
- Now we can finally get that decimal value! If you haven’t already, create a DecimalValue field and make it Calculated.
- Here’s my calculation. I’ll explain what the parts mean below
HexLookup N::DecValue + 16*HexLookup T::DecValue + 16^2* HexLookup C::DecValue + 16^3*HexLookup M::DecValue + 16^4*HexLookup D::DecValue+16^5*HexLookup X::DecValue
- “HexLookupN::DecValue” means give me the equivalent decimal value column based on the hex value in the “HexLookupN” (units digit) table instance.
- “HexLookup T::DecValue” does a look up for the tens unit. I multiply the value by 16 an add it to the ones value. Remember the hex #FF (F=15) means 15*16+15
- I look up the hundreds place decimal value and multiply it by 16^2 (256), then the thousands place decimal and multiply it by 16^3 (4096).
- I add up the results of each converted decimal digits times its appropriate power of 16.The calculation is complete.