[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Converting big numbers to hex

Posted on 2006-03-30
7
Medium Priority
?
831 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!

==============================================
0
Comment
Question by:bmatumbura
  • 3
  • 3
7 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16331868
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
 
LVL 11

Author Comment

by:bmatumbura
ID: 16331990
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
 
LVL 5

Expert Comment

by:AtanAsfaloth
ID: 16332078
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 11

Author Comment

by:bmatumbura
ID: 16332129
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
 
LVL 5

Accepted Solution

by:
AtanAsfaloth earned 260 total points
ID: 16332205
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
 
LVL 11

Author Comment

by:bmatumbura
ID: 16332268
I have increased the points. You deserve it! Thanks.
0
 
LVL 5

Expert Comment

by:AtanAsfaloth
ID: 16332321
You're welcome and thanks a lot :)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question