Solved

Scientific Number Conversions

Posted on 1998-07-27
2
449 Views
Last Modified: 2010-08-05
I need to convert a hex, octal, or binary number to DECIMAL.  How can I do this?  I know about the Hex and Oct functions, and can figure out decimal to binary, I just don't know how to get the number (string) back to decimal.  Also, if anyone has sample code for creating a scientific calculator (specifically the number conversions), I'd REALLY like to see it (and I'd pay extra points).
0
Comment
Question by:regent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Expert Comment

by:abrooke
ID: 1466873
This is the source I use to convert a hex stored as a string to a decimal long. How it's done: I iterate through the HexNumber from left to right each place has a value of 16^(total length minus current position in strength) I multiply this value times the actual value of the place 0-15. Add up the values and return the sum.

                                       Lonnie Brooke

Public Function HexToDecimal(HexNumber As String) As Long
   Dim tmpAr() As Byte
   Dim i&, j&, k&, t&, r&
   Dim l$, m$
   
   i = Len(HexNumber)
   ReDim tmpAr(i) As Byte
   j = 1
   k = i - 1
   While j < i + 1
      t = Asc(Mid(HexNumber, j, 1))
      Select Case t
         Case 65 To 70
            t = t - 55
         Case 48 To 57
            t = t - 48
      End Select
      r = r + ((16 ^ k) * t)
      k = k - 1
      j = j + 1
   Wend
   
   HexToDecimal = r
   
End Function

0
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1466874
The following function will convert a string representing a number in any base (2 through 36) to decimal. It handles illegal characters like Val(), by stopping the conversion.

Public Function ConvertToDecimal(sNumber As String, iBase As Integer) As Long
Dim lResult As Long
Dim i, iChar As Integer

    For i = 1 To Len(sNumber)
        iChar = Asc(UCase$(Mid$(sNumber, i, 1))) - 48
        If iChar > 9 Then iChar = iChar - 7
        If iChar < 0 Or iChar >= iBase Then Exit For 'invalid character
        lResult = lResult * iBase + iChar
    Next
    ConvertToDecimal = lResult
End Function

Hope this helps!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

615 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