Wednesday, October 1, 2008

Handling Large Hex in VBScript

VBScript doesn't inherently support hex constants over 8 bits in length so I made a little function that attempts to handle large Hex strings and convert them to Decimals. It should be noted that Doubles lose precision in the E15 range of magnitude so beware using anything too large:
Function HexToDec(strHex)
Dim i
Dim size
Dim ret
size = Len(strHex) - 1
ret = CDbl(0)
For i = 0 To size
ret = ret + CDbl("&H" & Mid(strHex, size - i + 1, 1)) * (CDbl(16) ^ CDbl(i))
Next
HexToDec = ret
End Function
Function DecToHex(dblNumber)
Dim Q
Dim ret

ret = ""
Q = CDbl(Fix(dblNumber))
While Q > 0
ret = Hex(Q - Fix(Q / 16) * 16) & ret
Q = Fix(Q / CDbl(16))
Wend
DecToHex = ret
End Function

Called like this:

ret = HexToDec("0C0BA715B2223D47A8CAC87BE5D9679")
ret = DecToHex(CDBL(16843216856846468461654862))

1 comment:

Jeanne Lawyer said...

??????????????????? Ben usually I get a kick out of your posts but today is a different language. Whatever Hex strings are I am sure they appreciate the post.