Visual Basic Programming Code Examples Visual Basic > Strings Code Examples Convert Hex string to Long Convert Hex string to Long converts a hexadecimal string into a long Returns zero if error occurs Public Function ConvertHexToLong(sHex As String) As Long On Error GoTo errHandler: Dim n As Integer Dim sTemp As String * 1 Dim nTemp As Integer Dim nFinal() As Integer Dim bNegative As Boolean ReDim nFinal(0) If LenB(sHex) = 0 Then ConvertHexToLong = 0 Exit Function End If bNegative = False For n = Len(sHex) To 1 Step -1 sTemp = Mid$(sHex, n, 1) nTemp = Val(sTemp) If nTemp = 0 Then Select Case UCase(sTemp) Case "A" nTemp = 10 Case "B" nTemp = 11 Case "C" nTemp = 12 Case "D" nTemp = 13 Case "E" nTemp = 14 Case "F" nTemp = 15 Case "-" bNegative = True Case Else nTemp = 0 End Select End If ReDim Preserve nFinal(UBound(nFinal) + 1) nFinal(UBound(nFinal)) = nTemp Next ConvertHexToLong = nFinal(1) For n = 2 To UBound(nFinal) ConvertHexToLong = ConvertHexToLong + (nFinal(n) * (4 ^ (n * 2 - 2))) Next If bNegative Then ConvertHexToLong = ConvertHexToLong - (ConvertHexToLong * 2) Exit Function errHandler: ConvertHexToLong = 0 End Function