How can I convert a hex number to a decimal number in Excel?

78,426

Solution 1

You could simply use:

HEX2DEC(right(A1,(len(A1)-2)))

len(A1) gives you the length of 0x string. Exclude the length of 0x, the remaining string is the hex number. Use Excel function to get the dec.

Solution 2

As TwiterZX indicated, Hex2Dec's input is limited to 10 characters and 6cd2c0306953 is 12 characters. So that won't work but let's roll our own function for that. Using VBA, add a Module and use the following code (may need to be adjusted based on your needs)

' Force explicit declaration of variables
Option Explicit


' Convert hex to decimal
' In:   Hex in string format
' Out:  Double
Public Function HexadecimalToDecimal(HexValue As String) As Double

    ' If hex starts with 0x, replace it with &H to represent Hex that VBA will understand
    Dim ModifiedHexValue As String
    ModifiedHexValue = Replace(HexValue, "0x", "&H")

    HexadecimalToDecimal = CDec(ModifiedHexValue)

End Function

In Excel, let's say cell A1 contains 0x00006cd2c0306953, A2's formula of =HexadecimalToDecimal(A1) will result in 1.19652E+14. Format the column to a number with zero decimals and the result will be 119652423330131.

Solution 3

HEX2DEC is limited to 10 characters, but that doesn't mean we can't use it. Simply use it several times, to convert 10 characters at a time and apply the appropriate power of 2 to each use.

= HEX2DEC(RIGHT(C8,10))+HEX2DEC(MID(C8,3,5))*POWER(16,10)

[Disclaimer: Untested at the moment]

Later: I'm now at a spreadsheet, ready to test. Change the 3,5 in MID to 3,6. Hmm.. Still not right.

Turns out that the HEX2DEC is working on signed hex values, so the first term ends up being negative. Not sure why, but here is the fixed version that adds 2^40 (or 16^10, as we're working in hex) to fix:

= HEX2DEC(RIGHT(C8,10))+POWER(16,10) + HEX2DEC(MID(C8,3,6))*POWER(16,10)

However, that only works if the RIGHT(C8,10) happens to be negative. Here's my general solution:

= HEX2DEC(RIGHT(C8,10))+IF(HEX2DEC(RIGHT(C8,10))<0,POWER(16,10),0) + HEX2DEC(MID(C8,3,6))*POWER(16,10)

Ugggh.

Solution 4

Try the following:

=HEX2DEC(RIGHT(D93;8))+HEX2DEC(LEFT(D93;LEN(D93)-8))*POWER(2;32)

Solution 5

One dirty way to perform this convertion, without using a function (see this excel forum thread for that) is to use this formula to compute the value of each character in the string, then sum those up. This obviously involves using temporary cells to decompose the number:

=HEX2DEC(LEFT(RIGHT(A$1,ROW()),1))*POWER(16,ROW()-1)

Assuming you place these temp cells on rows 1 to 16, this works by extracting each character, starting from the right, converting that to a value, then applying the relevant power of 16. Sum up all 16 cells in order to get your value.

Share:
78,426

Related videos on Youtube

Nathan Fellman
Author by

Nathan Fellman

SOreadytohelp

Updated on September 18, 2022

Comments

  • Nathan Fellman
    Nathan Fellman almost 2 years

    I have a cell containing this value:

    0x00006cd2c0306953
    

    and I want to convert it to a number.

    I tried:

    =HEX2DEC(C8)
    

    where C8 is the cell containing my hex value.

    I get a #NUM error.

    What am I doing wrong?

  • Nathan Fellman
    Nathan Fellman almost 12 years
    This doesn't work at all
  • Riad Krim
    Riad Krim almost 12 years
    I think this function is language dependent, what's the main language of your excel app, and your version..?
  • Nathan Fellman
    Nathan Fellman almost 12 years
    Office 2010 in English
  • Riad Krim
    Riad Krim almost 12 years
    HEX2DEC (or HEXDEC) function is limited to 40 bits (10 characters), and your number is 12 characters office.microsoft.com/en-us/excel-help/…
  • phuclv
    phuclv almost 9 years
    How does this language dependent? There's not even a function separator like , or ; here
  • phuclv
    phuclv almost 9 years
    why don't use 2^32?
  • phuclv
    phuclv almost 9 years
    Only the 40th bit is the sign bit, so you can just split the 64-bit value to two 32-bit values and it'll always be positive. Otherwise pad a zero to the hex string if it's shorter than 37 bits
  • phuclv
    phuclv almost 9 years
    IMHO 16^(ROW() - 1) is much cleaner and easier to read than POWER()
  • Scott - Слава Україні
    Scott - Слава Україні almost 8 years
    Have you tried this?  Have you read anything on this page?  It has been stated three times that HEX2DEC accepts a maximum of ten hex digits.  The string in the question, 0x00006cd2c0306953, is 18 characters long.  Your solution evaluates RIGHT(A1,(LEN(A1)-2)); 18−2 is 16, so RIGHT(…) returns 00006cd2c0306953, which is 16 digits long.  Even if you strip off the leading zeroes, you have 12 digits, which is still too many.  If you can get this to work, please describe your configuration.  (I.e., what version of Excel are you using?  And did you do anything tricky to get this to work?)
  • universe33
    universe33 almost 4 years
    f00000000000000000000000000000000000000000000000000000000000‌​0000 = 2.16E+164 ff0000000000000000000000000000000000000000000000000000000000‌​0000 = 2.29E+164 fff000000000000000000000000000000000000000000000000000000000‌​0000 = 2.30E+164 ffff00000000000000000000000000000000000000000000000000000000‌​0000 = 2.30E+164 fffff0000000000000000000000000000000000000000000000000000000‌​0000 = 2.30E+164 ffffff000000000000000000000000000000000000000000000000000000‌​0000 = 2.30E+164 fffffff00000000000000000000000000000000000000000000000000000‌​0000 = 2.30E+164