Excel: Converting Decimal to Hex and Hex to Decimal

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("x",1)
   A3:  =SET.NAME("n",INT(LOG(x)/LOG(16)))
   A4:  =SET.NAME("hex","")
   A5:  =WHILE(n>0)
   A6:  =SET.NAME("hex",hex&CHAR(INT(x/16^n)+48+
           IF(INT(x/16^n)>9,7,0)))
   A7:  =SET.NAME("x",MOD(x,16^n))
   A8:  =SET.NAME("n",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 "ToHex").

  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("x",2)
   B3:  =SET.NAME("dec",0)
   B4:  =FOR("i",1,LEN(x),1)
   B5:  =SET.NAME("n",CODE(MID(x,i,1)))
   B6:  =SET.NAME("dec",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 "ToDec").

  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 "Macro1", the ToHex function macro appears as as "Macro1!ToHex" in the Paste Function dialog box and the ToDec macro appears as "Macro1!ToDec".

  4. Click OK.

MORE INFORMATION

The ToHex macro's algorithm is as follows:

  1. Compute the largest power of 16 less than "x".

  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 "0"). 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.


KBCategory: kbother
KBSubcategory:

Additional reference words: 1.0 1.00 1.03 1.04 1.06 1.5 1.50 2.2
2.20 3.0 3.00

KBCategory: kbother
KBSubcategory:

Additional reference words: 1.50 2.20 3.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1994
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.