|
|
Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Does anyone know of an easy way to convert a Hex number to a Decimal? These numbers would be large, with a typical length of 13-15 characters.
Thanks!
|
|
"Jake" <Jake[ at ]discussions.microsoft.com> wrote in message news:BE93B79D-30DB-446B-93B8-D826A3A59DD7[ at ]microsoft.com...
[Quoted Text] > Does anyone know of an easy way to convert a Hex number to a Decimal? > These > numbers would be large, with a typical length of 13-15 characters. > > Thanks!
Val("&H" & [HexDigits])
Tom Lake
|
|
Jake wrote:
[Quoted Text] >Does anyone know of an easy way to convert a Hex number to a Decimal? These >numbers would be large, with a typical length of 13-15 characters.
Don't know what you can do with such a large number. but I think this gives you the best chance:
CDec("&H" & hexstringvariable)
-- Marsh MVP [MS Access]
|
|
If those don't work, let me know and I can post code tomorrow. Converting Hex to Decimal is fairly straightforward, though as previously pointed out, large numbers can be tricky to handle in VB, so I may be speaking too soon. :)
Rob
"Marshall Barton" <marshbarton[ at ]wowway.com> wrote in message news:4o4ph29crf7n5h0ip57bomgf0u10t0d8vp[ at ]4ax.com...
[Quoted Text] > Jake wrote: > >>Does anyone know of an easy way to convert a Hex number to a Decimal? >>These >>numbers would be large, with a typical length of 13-15 characters. > > > Don't know what you can do with such a large number. but I > think this gives you the best chance: > > CDec("&H" & hexstringvariable) > > -- > Marsh > MVP [MS Access]
|
|
The Val("&H...") function maxes out a little early for the size of numbers you're interested in. Here's a function that'll handle extremely large numbers:
Public Function HexToDec(ByVal strHex As String) As Variant Const cstrHexDigits As String = "0123456789ABCDEF" Dim decOut As Variant Dim i As Integer
decOut = CDec(0) For i = Len(strHex) To 1 Step -1 decOut = decOut + CDec(16 ^ (Len(strHex) - i)) * (InStr(cstrHexDigits, Mid$(strHex, i, 1)) - 1) Next HexToDec = decOut End Function
"Robert Morley" <rmorley[ at ]magma.ca.N0.Freak1n.sparn> wrote in message news:%23zgt6G44GHA.2208[ at ]TK2MSFTNGP04.phx.gbl...
[Quoted Text] > If those don't work, let me know and I can post code tomorrow. Converting > Hex to Decimal is fairly straightforward, though as previously pointed > out, large numbers can be tricky to handle in VB, so I may be speaking too > soon. :) > > > Rob > > "Marshall Barton" <marshbarton[ at ]wowway.com> wrote in message > news:4o4ph29crf7n5h0ip57bomgf0u10t0d8vp[ at ]4ax.com... >> Jake wrote: >> >>>Does anyone know of an easy way to convert a Hex number to a Decimal? >>>These >>>numbers would be large, with a typical length of 13-15 characters. >> >> >> Don't know what you can do with such a large number. but I >> think this gives you the best chance: >> >> CDec("&H" & hexstringvariable) >> >> -- >> Marsh >> MVP [MS Access] > >
|
|
Thanks Robert - it worked perfectly!
"Robert Morley" wrote:
[Quoted Text] > The Val("&H...") function maxes out a little early for the size of numbers > you're interested in. Here's a function that'll handle extremely large > numbers: > > Public Function HexToDec(ByVal strHex As String) As Variant > Const cstrHexDigits As String = "0123456789ABCDEF" > Dim decOut As Variant > Dim i As Integer > > decOut = CDec(0) > For i = Len(strHex) To 1 Step -1 > decOut = decOut + CDec(16 ^ (Len(strHex) - i)) * > (InStr(cstrHexDigits, Mid$(strHex, i, 1)) - 1) > Next > HexToDec = decOut > End Function > > "Robert Morley" <rmorley[ at ]magma.ca.N0.Freak1n.sparn> wrote in message > news:%23zgt6G44GHA.2208[ at ]TK2MSFTNGP04.phx.gbl... > > If those don't work, let me know and I can post code tomorrow. Converting > > Hex to Decimal is fairly straightforward, though as previously pointed > > out, large numbers can be tricky to handle in VB, so I may be speaking too > > soon. :) > > > > > > Rob > > > > "Marshall Barton" <marshbarton[ at ]wowway.com> wrote in message > > news:4o4ph29crf7n5h0ip57bomgf0u10t0d8vp[ at ]4ax.com... > >> Jake wrote: > >> > >>>Does anyone know of an easy way to convert a Hex number to a Decimal? > >>>These > >>>numbers would be large, with a typical length of 13-15 characters. > >> > >> > >> Don't know what you can do with such a large number. but I > >> think this gives you the best chance: > >> > >> CDec("&H" & hexstringvariable) > >> > >> -- > >> Marsh > >> MVP [MS Access] > > > > > > >
|
|
The one limitation of it, which I should've mentioned, is that it'll happily screw up royally if you try to pass it a "normal" string...such as HexToDec("Jackdaws love my big sphinx of quartz"). If there's any chance that what gets passed won't be a hex number, you'll want to add some checking to the function.
Rob
"Jake" <Jake[ at ]discussions.microsoft.com> wrote in message news:CDC0D024-B0C1-4D40-848B-031A12B61F1F[ at ]microsoft.com...
[Quoted Text] > Thanks Robert - it worked perfectly! > > "Robert Morley" wrote: > >> The Val("&H...") function maxes out a little early for the size of >> numbers >> you're interested in. Here's a function that'll handle extremely large >> numbers: >> >> Public Function HexToDec(ByVal strHex As String) As Variant >> Const cstrHexDigits As String = "0123456789ABCDEF" >> Dim decOut As Variant >> Dim i As Integer >> >> decOut = CDec(0) >> For i = Len(strHex) To 1 Step -1 >> decOut = decOut + CDec(16 ^ (Len(strHex) - i)) * >> (InStr(cstrHexDigits, Mid$(strHex, i, 1)) - 1) >> Next >> HexToDec = decOut >> End Function >> >> "Robert Morley" <rmorley[ at ]magma.ca.N0.Freak1n.sparn> wrote in message >> news:%23zgt6G44GHA.2208[ at ]TK2MSFTNGP04.phx.gbl... >> > If those don't work, let me know and I can post code tomorrow. >> > Converting >> > Hex to Decimal is fairly straightforward, though as previously pointed >> > out, large numbers can be tricky to handle in VB, so I may be speaking >> > too >> > soon. :) >> > >> > >> > Rob >> > >> > "Marshall Barton" <marshbarton[ at ]wowway.com> wrote in message >> > news:4o4ph29crf7n5h0ip57bomgf0u10t0d8vp[ at ]4ax.com... >> >> Jake wrote: >> >> >> >>>Does anyone know of an easy way to convert a Hex number to a Decimal? >> >>>These >> >>>numbers would be large, with a typical length of 13-15 characters. >> >> >> >> >> >> Don't know what you can do with such a large number. but I >> >> think this gives you the best chance: >> >> >> >> CDec("&H" & hexstringvariable) >> >> >> >> -- >> >> Marsh >> >> MVP [MS Access] >> > >> > >> >> >>
|
|
=?Utf-8?B?SmFrZQ==?= <Jake[ at ]discussions.microsoft.com> wrote in news:BE93B79D-30DB-446B-93B8-D826A3A59DD7[ at ]microsoft.com:
[Quoted Text] > Does anyone know of an easy way to convert a Hex number to a Decimal? > These numbers would be large, with a typical length of 13-15 > characters. >
This will deal with arbitrary length, as long as you don't exceed the size of a Double:
Public Function HexToDouble(SomeHex As String) As Double
Const digits As String = "0123456789ABCDEF" If Len(SomeHex) = 0 Then HexToDouble = 0 Else HexToDouble = _ HexToDouble(Left(SomeHex, Len(SomeHex) - 1)) * 16 + _ InStr(digits, Mid(SomeHex, Len(SomeHex), 1)) - 1 End If End Function
There's no error trapping for illegal digits, you just get the wrong answer.
Hope it helps
Tim F
|
|
|