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:
- Select the cell A1 (the cell containing "ToHex").
- From the Formula menu, choose Define Name.
- 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:
- Select the cell B1 (the cell containing "ToDec").
- From the Formula menu, choose Define Name.
- In the Macro box, select Function and click OK.
To run the macros:
- Select a cell on the worksheet to contain the converted value.
- From the Formula menu, choose Paste Function.
- 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".
- Click OK.
MORE INFORMATION
The ToHex macro's algorithm is as follows:
- Compute the largest power of 16 less than "x".
- Initialize hex (the return string) to null.
- Assume that x is greater than 15 (more than one hex digit remaining
to process...).
- 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().
- Subtract the current hex digit from x and decrement the power
of 16 (contained in n).
- End the loop.
- Append the final hex digit (the case of n=0) onto the hex string.
The ToDec macro's algorithm is as follows:
- Initialize dec (the return integer) to zero.
- Set up a loop to count each character in the text string.
- Get the ASCII number of the current hex digit.
- Multiply the current total by 16 and add the next hex digit (the
IF() calculates the offset for A-F).
- End the loop and return to dec.
|