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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert HEX to Decimal

Status
Not open for further replies.

rhack5565

Technical User
Feb 15, 2002
69
US
Is there a function that will convert from Hex to Decimal? I see a HEX(decimal#) function which will take a decimal number and convert to Hex, but can't seem to find a function that will do the reverse.

 
Try this:


Dim NextHexDigit As Double
Dim HexNum As String
HexNum = ""


While DecNum <> 0
NextHexDigit = DecNum - (Int(DecNum / 16) * 16)


If NextHexDigit < 10 Then
HexNum = Chr(Asc(NextHexDigit)) & HexNum
Else
HexNum = Chr(Asc(&quot;A&quot;) + NextHexDigit - 10) & HexNum
End If
DecNum = Int(DecNum / 16)
Wend
If HexNum = &quot;&quot; Then HexNum = &quot;0&quot;
BigDecToHex = HexNum
End Function


Hope it Helps,

John Stephens
 
That was quick. I'll give it a try tomorrow and see how it works. Thanks for the help!
 
or try
Code:
Option Explicit

Private Sub Command1_Click()
  MsgBox Hex2Dec(Text1.Text)
End Sub

Private Function Hex2Dec(ByVal sHex As String) As Long
  On Error Resume Next
  Hex2Dec = Val(&quot;&H&quot; & sHex)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top