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

on
Medium Priority
884 Views
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

## View Solution Only

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

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.

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

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

Commented:
I have increased the points. You deserve it! Thanks.

Commented:
You're welcome and thanks a lot :)
##### Thanks for using Experts Exchange.

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