Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert hexadecimal to decimal

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
Hi All,

I have a string of 8 characters, which is supposed to represent a hexadecimal number. Does anyone know of any easy or smart way of converting a hexadecimal string into a decimal number of type integer ?

Thanks in advance,

James
 
Here's a response I found on another board (cant take credit myself :-( )

You may have to work with the result to convert to type Int:

sHexValue is your 8 char string
Debug.Print = Val("&H" & sHexValue & "&")

Here's where i found it, u may have to cut and paste:

Goodluck

Conrad
 
How about:

lngMyInt = format("&h" & str8chars, "########")

?
 
Code:
Private Function HexToDec(ByVal strNum As String) As String
'converts hexadecimal to decimal
    Dim intLength As Integer 'length of text box
    Dim i As Integer         'counter
    Dim lngNumber As Long    'value of each character
    Dim dblSum As Double     'answer
    Dim strTemp As String    'variable that holds each character
    intLength = Len(Trim(txtInput.Text))
    
    'looping length of text box to convert characters 1 by 1
    For i = intLength To 1 Step -1
    
        strTemp = Mid(strNum, i, 1)
        
        'case statement to convert alpha charachters to numeric
        Select Case strTemp
    
            Case "a"
            lngNumber = 10
    
            Case "b"
            lngNumber = 11
    
            Case "c"
            lngNumber = 12
    
            Case "d"
            lngNumber = 13
    
            Case "e"
            lngNumber = 14
    
            Case "f"
            lngNumber = 15
    
            Case Else
            lngNumber = Val(strTemp)
    
        End Select
    
        dblSum = dblSum + (lngNumber * (16 ^ (intLength - i)))
    
    Next i
    
    HexToDec = CStr(dblSum)
 
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top