bmatumbura

asked on

# 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!

==============================================

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!

==========================

ASKER

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

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

ASKER

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?

ASKER CERTIFIED SOLUTION

membership

Create a free account to see this answer

Signing up is free and takes 30 seconds.

**No credit card required.**ASKER

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

You're welcome and thanks a lot :)

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