I'm looking to convert an auto-number field value to a hex string formula field. My auto-number is not going to be larger than 999,999. I found some suggestions on the net (thanks Chris Gary) and came up with this formula:

case(mod( floor(Decimal_Number__c/65536) ,16),0,"0",1,"1",2,"2",3,"3",4,"4",5,"5",6,"6",7,"7",8,"8",9,"9",10,"A",11,"B",12,"C",13,"D",14,"E",15,"F","")
case(mod( floor(Decimal_Number__c/4096) ,16),0,"0",1,"1",2,"2",3,"3",4,"4",5,"5",6,"6",7,"7",8,"8",9,"9",10,"A",11,"B",12,"C",13,"D",14,"E",15,"F","")
case(mod( floor(Decimal_Number__c/256) ,16),0,"0",1,"1",2,"2",3,"3",4,"4",5,"5",6,"6",7,"7",8,"8",9,"9",10,"A",11,"B",12,"C",13,"D",14,"E",15,"F","")
case(mod( floor(Decimal_Number__c/16) ,16),0,"0",1,"1",2,"2",3,"3",4,"4",5,"5",6,"6",7,"7",8,"8",9,"9",10,"A",11,"B",12,"C",13,"D",14,"E",15,"F","")

A value of 999999 in my number field gives F423F in my hex string formula field. I'm wondering if anyone has a better approach.

  • That's about as efficient a way as I can see. I might spell the divisor out in terms of 16 -- 16^0, 16^1, 16^2, ... -- to make it clearer what's happening there, but that's just me being silly. – Shane Steinfeld Jan 10 '19 at 19:02
  • Putting it into play I found that my test formula here worked because field 'Decimal_Number__c' is a number field. When I switched over to an auto-number field I had to use the value function: case(mod( floor(value(autoNumberField__c)/65536) ,16), etc. – Dean Wooldridge Jan 10 '19 at 21:36

With a formula field, I don't think you could do much better in terms of readability or compiled size.

You may be able to find a way to reduce some of the repetition, you could use POW(16,<integer>) to replace the Decimal_Number__c/<x> where x is 16, 256, 4096, 65536. Really, any changes you make to the formula here are going to be a trade-off between compile size, repetition, and readability/maintainability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.