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?
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?
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.
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.
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.
Long formula but it is working for 64-HEX characters:
=HEX2DEC(MID(A24,1,8))*2^512 **(*4)** +HEX2DEC(MID(A24,9,8))*2^512 **(*2)** +HEX2DEC(MID(A24,17,8))*2^512+HEX2DEC(MID(A24,25,8))*2^256+HEX2DEC(MID(A24,33,8))*2^128+HEX2DEC(MID(A24,41,8))*2^64+HEX2DEC(MID(A24,49,8))*2^32+HEX2DEC(MID(A24,57,8))
please note:
(*4) = *4 (remove brackets)
and:
(*2) = *2 (remove brackets)
also note: all 64 character must be present like the following example:
0000000000000000000000000000000000000000000000000000000000000fd1
Cheers
Make sure your column which has HEX numbers is not having 0x. The HEX number should be C8, not 0xC8. Hope it helps.
HEX2DEC fails if there are any leading or trailing non-hex (ABCDEF0123456789) characters. Spaces are a real landmine because they are not visible. Often hex numbers, represented as strings, in log files contain leading and/or trailing spaces. I find that "=HEX2DEC(TRIM(A1)) will usually fix this problem. However, something like "=HEX2DEC(RIGHT(TRIM(A1), 10), may be necessary due to the 10 character limitation of HEX2DEC.
This function has changed try using
=HEXDEC(C8)
so without number 2 between HEX & DEC