Microsoft KB Archive/46518

{| = Excel: Converting Decimal to Hex and Hex to Decimal =
 * width="100%"|

Last reviewed: November 2, 1994

Article ID: Q46518 The information in this article applies to:
 * Microsoft Excel for the Macintosh, versions 1.x, 2.2, 3.0

SUMMARY
To convert decimal integers into their hexadecimal equivalents as text, enter the following commands into a macro sheet:

A1: ToHex A2: =ARGUMENT(&quot;x&quot;,1) A3: =SET.NAME(&quot;n&quot;,INT(LOG(x)/LOG(16))) A4: =SET.NAME(&quot;hex&quot;,&quot;&quot;) A5: =WHILE(n>0) A6: =SET.NAME(&quot;hex&quot;,hex&CHAR(INT(x/16^n)+48+ IF(INT(x/16^n)>9,7,0))) A7: =SET.NAME(&quot;x&quot;,MOD(x,16^n)) A8: =SET.NAME(&quot;n&quot;,n-1) A9: =NEXT A10: =RETURN(hex&CHAR(x+48+IF(x>9,7,0))) To define the macro, do the following:
 * 1) Select the cell A1 (the cell containing &quot;ToHex&quot;).
 * 2) From the Formula menu, choose Define Name.
 * 3) In the Macro box, select Function and click OK.

To convert hexadecimal strings into their decimal integer equivalents, enter the following commands into a macro sheet: B1: ToDec B2: =ARGUMENT(&quot;x&quot;,2) B3: =SET.NAME(&quot;dec&quot;,0) B4: =FOR(&quot;i&quot;,1,LEN(x),1) B5: =SET.NAME(&quot;n&quot;,CODE(MID(x,i,1))) B6: =SET.NAME(&quot;dec&quot;,dec*16+n-48-IF(n>57,39,0)) B7: =NEXT B8: =RETURN(dec) To define the macro, do the following:
 * 1) Select the cell B1 (the cell containing &quot;ToDec&quot;).
 * 2) From the Formula menu, choose Define Name.
 * 3) In the Macro box, select Function and click OK.

To run the macros:
 * 1) Select a cell on the worksheet to contain the converted value.
 * 2) From the Formula menu, choose Paste Function.
 * 3) Choose the appropriate function from the list. For example, if your macro sheet is named &quot;Macro1&quot;, the ToHex function macro appears as as &quot;Macro1!ToHex&quot; in the Paste Function dialog box and the ToDec macro appears as &quot;Macro1!ToDec&quot;.
 * 4) Click OK.

MORE INFORMATION
The ToHex macro's algorithm is as follows:


 * 1) Compute the largest power of 16 less than &quot;x&quot;.
 * 2) Initialize hex (the return string) to null.
 * 3) Assume that x is greater than 15 (more than one hex digit remaining to process...).
 * 4) Compute the current hex digit. The current hex digit is based on the current value of x divided by 16^n, so add this number to 48 (ASCII of &quot;0&quot;). Then, if x is greater than 9, convert to A-F by adding 7 more. Finally, convert this integer to text using CHAR.
 * 5) Subtract the current hex digit from x and decrement the power of 16 (contained in n).
 * 6) End the loop.
 * 7) Append the final hex digit (the case of n=0) onto the hex string.

The ToDec macro's algorithm is as follows:
 * 1) Initialize dec (the return integer) to zero.
 * 2) Set up a loop to count each character in the text string.
 * 3) Get the ASCII number of the current hex digit.
 * 4) Multiply the current total by 16 and add the next hex digit (the IF calculates the offset for A-F).
 * 5) End the loop and return to dec.
 * }