Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Strings Code Examples

Converting a hex string to a long

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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