IsHex()
I will begin writing one myself, but if anyone already has one, I'd like to use it.
Thanks!
Dave
Printable View
IsHex()
I will begin writing one myself, but if anyone already has one, I'd like to use it.
Thanks!
Dave
This pair of functions takes care of it. You could probably write something better...
VB Code:
Option Explicit ' ' Private Function IsByteHex(char As Variant) As Boolean ' Checks if char is a Hexidecimal digit ' IsByteHex = False ' If IsNumeric(char) = True Then IsByteHex = True ElseIf char = "a" Then IsByteHex = True ElseIf char = "b" Then IsByteHex = True ElseIf char = "c" Then IsByteHex = True ElseIf char = "d" Then IsByteHex = True ElseIf char = "e" Then IsByteHex = True ElseIf char = "f" Then IsByteHex = True End If ' End Function Public Function IsHex(strInput As String) As Boolean ' Checks if strInput is a Hexidecimal number ' Dim i As Integer Dim blnIsHex As Boolean Dim char As Variant ' IsHex = False blnIsHex = True ' If Len(strInput) = 0 Then blnIsHex = False End If ' For i = 1 To Len(strInput) ' char = CVar(Mid(strInput, i, 1)) ' If IsByteHex(char) = True Then blnIsHex = (blnIsHex And True) Else blnIsHex = (blnIsHex And False) End If Next ' IsHex = blnIsHex ' End Function
I may be too late in responding, but...VB Code:
Public Function IsHex(strInput As String) As Boolean Dim i As Long IsHex = False If Len(strInput) = 0 Then Exit Function For i = 1 To Len(strInput) If Not (Mid$(strInput, i, 1) Like "[0-9a-hA-H]") Then Exit Function Next IsHex = True End Function
optimized code:
VB Code:
Public Function IsHex(strInput As String) As Boolean Dim I As Long Dim J as Long If LenB(strInput) = 0 Then Exit Function End If I = 1 J = Len(strInput) Do Until I > J If Not (Mid$(strInput, I, 1) Like "[0-9a-hA-H]") Then Exit Function End If I = I + 1 Loop IsHex = True End Function
Thanks guys, I like your functions better than mine. I never used the Like operator before...
I didn't understand this part:
" If IsNumeric(char) = True Then
IsByteHex = True
"
Just because a number contains "0" doesn't make it Hex. I.e., "20198"
20198 is valid Hex though, just because it doesn't contain any letters doesn't mean it's not Hex. The code above just tests whether a string contains only numbers and letters A through F.
Okay, yes it could be hex - lol. In my case, I have a program that tests an instrument through a serial port. It mostly responds in decimal, but every now and then it "burbs" and responds in HEX.
I had a similar For next loop to look for A-F, then I know the instrument burped, and to process the response accordingly.
I went searching to see if there was a built-in "IsHex" function (which there isn't) and found this old message string.
The bad thing is if the instrument "burps" and responds in hex, but without the A-F, I am going to get some wild data. - Hah, just ran in to that, but the program does detect Bull Shite data, well, mostly does.
I do like the "Like" statement, but for now will stick with the for next statement
These ancient threads are always recipe for disaster. You'll be better off if you don't use any code out of these without applying common sense first.
Btw, here is non-optimized straight-forward no-loop one-liner of an IsHex that might be useful to someone
It's like a double negative i.e. "does not contain nothing by hex digits".Code:Option Explicit
Private Sub Form_Load()
Debug.Print IsHex("A0")
End Sub
Public Function IsHex(ByVal sText As String) As Boolean
If LenB(sText) <> 0 Then
IsHex = Not (sText Like "*[!0-9a-fA-F]*")
End If
End Function
cheers,
</wqw>
p.s. For decimal numbers it's similar: IsDec = Not sText Like "*[!0-9]*"