API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
 
Decimal To Hex Conversion
A recent project required a decimal to hex conversion. In LotusScript, this is very basic. But this needed to be done in formula language for this particular application. So we wrote a decimal to hex conversion using looping in the R6 formula language.

Since just about every calculator out there does this kind of conversion, it's not often you have to think about really how it's done. What you really are doing is working backwards through the final result. Let's take the number 86185 and convert it to hex. You take the number and divide by 16. In our example, that's 5386.5625 (86185/16). The remainder is converted to hex (.5625 = 9/16, so "9") and placed at the front of the return string. The whole number piece (5386) is then divided by 16 (336.625) and again the remainder (.625 = 10/16, so "A") is placed at the front of the return string. This process continues until the whole number is zero.

Hopefully you were able to follow that example above. But, if not, let's go through 86185 again in an example:

Result = ""
Value = 86185
Value / 16 = 5386.5625, Whole = 5386, Remainder = 9
Remainder converted to hex = "9", Result = "9"
Value = Whole = 5386
Value / 16 = 336.625, Whole = 336, Remainder = 10
Remainder converted to hex = "A", Result = "A9"
Value = Whole = 336
Value / 16 = 21, Whole = 21, Remainder = 0
Remainder converted to hex = "0", Result = "0A9"
Value = Whole = 21
Value / 16 = 1.3125, Whole = 1, Remainder = 5
Remainder converted to hex = "5", Result = "50A9"
Value = Whole = 1
Value / 16 = 0.0625, Whole = 0, Remainder = 1
Remainder converted to hex = "1", Result = "150A9"
Value = Whole = 0, which terminates our loop

Now, we need to accomplish this same task using formula language. Looping in the R6 formula language allows us to accomplish this task.

Decimal := <value to be converted, probably comes from a field>;
Hex := @If(Decimal = 0; "0"; "");
@While(Decimal > 0;
    Whole := @Integer(Decimal/16);
    Rest := Decimal - (Whole*16);
    Hex := @If(Rest < 10; @Text(Rest); @Char(65+Rest-10)) + Hex;
    Decimal := Whole
);
Hex

The @Char statement converts numbers 10 and larger to their hex equivalent. 65 is the ASCII value for "A". So, if Rest is 11, then we get @Char(65+11-10) which is @Char(66), which is "B". Yes, I could have used math and did 55 + Decimal. If I really wanted to be technical and reusable, though, I should have had this code up above my conversion:

LetterA := 1;
@While(@Char(LetterA) != "A"; LetterA := LetterA + 1);

Then, I would use the value LetterA instead of the value 65 in the code. That would truly be reusable - it would compute at run time the ASCII value of the letter A.

Let's run through the code again with our 86185 example. Decimal is 86185 and Hex is the empty string.

In the first time through the @While loop, Whole is 5386, and Rest is 9. This causes Hex to become "9" and Decimal is now 5386.
In the second time through the @While loop, Whole is 336, and Rest is 10. This causes Hex to become "A9" and Decimal is now 336.
In the third time through the @While loop, Whole is 21, and Rest is 0. This causes Hex to become "0A9" and Decimal is now 21.
In the fourth time through the @While loop, Whole is 1, and Rest is 5. This causes Hex to become "50A9" and Decimal is now 1.
In the fifth (and final) time through the @While loop, Whole is 0, and Rest is 1. This causes Hex to become "150A9" and Decimal is now 0.
Since Decimal is zero, the @While loop terminates and we have our result ("150A9") stored in the variable Hex.