-
Jan 18th, 2005, 01:57 PM
#1
Thread Starter
PowerPoster
Anybody have a IsHex() function? [Resolved]
IsHex()
I will begin writing one myself, but if anyone already has one, I'd like to use it.
Thanks!
Dave
Last edited by Dave Sell; Jan 18th, 2005 at 02:38 PM.
-
Jan 18th, 2005, 02:39 PM
#2
Thread Starter
PowerPoster
Re: Anybody have a IsHex() function? [Resolved]
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
-
Jan 18th, 2005, 02:58 PM
#3
Hyperactive Member
Re: Anybody have a IsHex() function? [Resolved]
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
-
Jan 18th, 2005, 03:03 PM
#4
Banned
Re: Anybody have a IsHex() function? [Resolved]
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
-
Jan 18th, 2005, 04:16 PM
#5
Thread Starter
PowerPoster
Re: Anybody have a IsHex() function? [Resolved]
Thanks guys, I like your functions better than mine. I never used the Like operator before...
-
Oct 9th, 2024, 01:30 PM
#6
Addicted Member
Re: Anybody have a IsHex() function? [Resolved]
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"
-
Oct 9th, 2024, 01:40 PM
#7
Re: Anybody have a IsHex() function? [Resolved]
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.
-
Oct 9th, 2024, 01:56 PM
#8
Addicted Member
Re: Anybody have a IsHex() function? [Resolved]
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
Last edited by daveyk021; Oct 9th, 2024 at 02:03 PM.
-
Oct 10th, 2024, 07:25 AM
#9
Re: Anybody have a IsHex() function? [Resolved]
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
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
It's like a double negative i.e. "does not contain nothing by hex digits".
cheers,
</wqw>
p.s. For decimal numbers it's similar: IsDec = Not sText Like "*[!0-9]*"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|