VB and VBA Users Source Code: Converting a hex string to a long
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Converting a hex string to a long
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, January 08, 2005
Hits:
2754
Category:
Visual Basic General
Article:
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. 'Author : Andrew Baker 'Date : 5/Aug/2001 '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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder