Convert Hex to Decimal (32-bit Unsigned)

By | 2002-06-01

Converts Hex [0 – FFFFFFFF] to Decimal [0 – 4294967295] using Currency type to avoid the sign bit.

Original Author: Larry Serflaten

Inputs

A valid 1-8 character Hex String

Returns

A Currency value in the range of 0 – 4294967295

Code

Function ConvertHex (H$) As Currency
Dim Tmp$
Dim lo1 As Integer, lo2 As Integer
Dim hi1 As Long, hi2 As Long
Const Hx = "&H"
Const BigShift = 65536
Const LilShift = 256, Two = 2
  Tmp = H
  'In case "&H" is present
  If UCase(Left$(H, 2)) = "&H" Then Tmp = Mid$(H, 3)
  'In case there are too few characters
  Tmp = Right$("0000000" & Tmp, 8)
  'In case it wasn't a valid number
  If IsNumeric(Hx & Tmp) Then
    lo1 = CInt(Hx & Right$(Tmp, Two))
    hi1 = CLng(Hx & Mid$(Tmp, 5, Two))
    lo2 = CInt(Hx & Mid$(Tmp, 3, Two))
    hi2 = CLng(Hx & Left$(Tmp, Two))
    ConvertHex = CCur(hi2 * LilShift + lo2) * BigShift + (hi1 * LilShift) + lo1
  End If
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.