Converting big numbers to hex

Dear Experts,

I need a function that can convert big numbers into hex representation and vice versa.

The Hex$ function only works for small numbers. For big numbers like 12345678901234, Hex$ throws an Overflow error.

Here is what I have been trying to do:

==============================================
Dim sHex As String, C as Currency

C = 12345678901234

sHex = Hex$(C) ' I get an Overflow error on this statement!

==============================================
LVL 11
bmatumburaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hi bmatumbura,

Hex returns the hexadecimal representation of a Long data type.

The biggest positive number a Long can contain is 2,147,483,647.  Your number is larger than that (by a few orders
of magnitude).

Regards,

Patrick
0
bmatumburaAuthor Commented:
Yes, I understand that. What I need is a user defined function that works more like Hex$, but can accept big numbers beyong the Long data type e.g the Currency data type, which can store numbers up to 922,337,203,685,477.
0
AtanAsfalothCommented:
Does this function help?

Public Function BigHex(ByVal varDecimal As Variant) As String
    'Declare Vars
    Dim intLoopLength As Integer
    Dim strHex As String
    Dim I As Integer
    '
    'Calculate number of hex digits
    intLoopLength = Int(Log(varDecimal) / Log(16))
    '
    strHex = ""
    For I = intLoopLength To 0 Step -1
        strHex = strHex & Hex(Int(varDecimal / (16 ^ I)))
        varDecimal = varDecimal - Int(varDecimal / (16 ^ I)) * (16 ^ I)
    Next I
    '
    'Return hex value
    BigHex = strHex
End Function
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

bmatumburaAuthor Commented:
Voila! It works, although I need to verify the results of the computations. Now for the 2nd part, How do I convert this Hex value back into the big number?
0
AtanAsfalothCommented:
Here you go :)


Public Function BigHexToDec(ByVal strHex As String) As Variant
    'Declare Vars
    Dim varDecimal As Variant
    Dim I As Integer
    '
    'Calculate Decimal
    varDecimal = 0
    For I = 1 To Len(strHex)
        varDecimal = varDecimal + Int("&H" & Mid(strHex, I, 1)) * 16 ^ (Len(strHex) - I)
    Next I
    '
    'Return number
    BigHexToDec = varDecimal
End Function


Regards,

Atan Asfaloth
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bmatumburaAuthor Commented:
I have increased the points. You deserve it! Thanks.
0
AtanAsfalothCommented:
You're welcome and thanks a lot :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.