We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Converting big numbers to hex

bmatumbura
bmatumbura asked
on
Medium Priority
884 Views
Last Modified: 2012-06-21
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!

==============================================
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

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.
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

Author

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?
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I have increased the points. You deserve it! Thanks.
You're welcome and thanks a lot :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.