Results 1 to 9 of 9

Thread: Anybody have a IsHex() function? [Resolved]

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Resolved 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.

  2. #2

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Anybody have a IsHex() function? [Resolved]

    This pair of functions takes care of it. You could probably write something better...
    VB Code:
    1. Option Explicit
    2. '
    3. '
    4.  
    5. Private Function IsByteHex(char As Variant) As Boolean
    6.     ' Checks if char is a Hexidecimal digit
    7.     '
    8.     IsByteHex = False
    9.     '
    10.     If IsNumeric(char) = True Then
    11.         IsByteHex = True
    12.     ElseIf char = "a" Then
    13.         IsByteHex = True
    14.     ElseIf char = "b" Then
    15.         IsByteHex = True
    16.     ElseIf char = "c" Then
    17.         IsByteHex = True
    18.     ElseIf char = "d" Then
    19.         IsByteHex = True
    20.     ElseIf char = "e" Then
    21.         IsByteHex = True
    22.     ElseIf char = "f" Then
    23.         IsByteHex = True
    24.     End If
    25.     '
    26. End Function
    27.  
    28. Public Function IsHex(strInput As String) As Boolean
    29.     ' Checks if strInput is a Hexidecimal number
    30.     '
    31.     Dim i As Integer
    32.     Dim blnIsHex As Boolean
    33.     Dim char As Variant
    34.     '
    35.     IsHex = False
    36.     blnIsHex = True
    37.     '
    38.     If Len(strInput) = 0 Then
    39.         blnIsHex = False
    40.     End If
    41.     '
    42.     For i = 1 To Len(strInput)
    43.         '
    44.         char = CVar(Mid(strInput, i, 1))
    45.         '
    46.         If IsByteHex(char) = True Then
    47.             blnIsHex = (blnIsHex And True)
    48.         Else
    49.             blnIsHex = (blnIsHex And False)
    50.         End If
    51.     Next
    52.     '
    53.     IsHex = blnIsHex
    54.     '
    55. End Function

  3. #3
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Anybody have a IsHex() function? [Resolved]

    I may be too late in responding, but...
    VB Code:
    1. Public Function IsHex(strInput As String) As Boolean
    2.   Dim i As Long
    3.  
    4.   IsHex = False
    5.  
    6.   If Len(strInput) = 0 Then Exit Function
    7.  
    8.   For i = 1 To Len(strInput)
    9.     If Not (Mid$(strInput, i, 1) Like "[0-9a-hA-H]") Then Exit Function
    10.   Next
    11.  
    12.   IsHex = True
    13. End Function

  4. #4

    Re: Anybody have a IsHex() function? [Resolved]

    optimized code:
    VB Code:
    1. Public Function IsHex(strInput As String) As Boolean
    2.  Dim I As Long
    3.  Dim J as Long
    4.  
    5.  If LenB(strInput) = 0 Then
    6.   Exit Function
    7.  End If
    8.  
    9.  I = 1
    10.  J = Len(strInput)
    11.  Do Until I > J
    12.   If Not (Mid$(strInput, I, 1) Like "[0-9a-hA-H]") Then
    13.    Exit Function
    14.   End If
    15.  
    16.   I = I + 1
    17.  Loop
    18.  
    19.  IsHex = True
    20. End Function

  5. #5

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Anybody have a IsHex() function? [Resolved]

    Thanks guys, I like your functions better than mine. I never used the Like operator before...

  6. #6
    Addicted Member
    Join Date
    Sep 2022
    Posts
    130

    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"

  7. #7
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,086

    Talking 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.

  8. #8
    Addicted Member
    Join Date
    Sep 2022
    Posts
    130

    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.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,523

    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
  •  



Click Here to Expand Forum to Full Width