Visual Basic Programming Code Examples Visual Basic > Strings Code Examples Converting a hex string to a long Converting a hex string to a long Below is a routine for converting a hex string to a long. 'Purpose : Converts a Hex string value to a long 'Inputs : sHex The hex value to convert to a long. eg. "H1" or "&H1". 'Outputs : Returns the numeric value of a string containing a hex value. 'Notes : Public Function CHex(sHex As String) As Long Dim iNegative As Integer, sPrefixH As String On Error Resume Next iNegative = CBool(Left$(sHex, 1) = "-") sPrefixH = IIf(InStr(1, sHex, "H", vbTextCompare), "", "H") If iNegative Then 'Negative number If Mid$(sHex, 2, 1) = "&" Then CHex = CLng("&" & sPrefixH & Mid$(sHex, 3)) * iNegative Else 'Append the ampersand to enable CLng to convert the value CHex = CLng("&" & sPrefixH & Mid$(sHex, 2)) * iNegative End If Else 'Positive number If Left$(sHex, 1) = "&" Then CHex = CLng("&" & sPrefixH & Mid$(sHex, 2)) Else 'Append the ampersand to enable CLng to convert the value CHex = CLng("&" & sPrefixH & sHex) End If End If End Function