If find it odd that the .FromHex function will return a value from a string that has non-hex characters. For example, Integer.FromHex(“Jon”) returns 5271. I don’t understand why this doesn’t throw an error.
In a project I want to enter hex bytes through a text field but need to verify them before converting and passing on to a serial device. I came up with this:
Function IsHexByte(s As String) As Boolean
s = s.Uppercase.Trim
If s.IsEmpty Or (s.Bytes > 2) Then
Return False
End If
Var HexDigits As String = "0123456789ABCDEF"
If HexDigits.IndexOf(s.Middle(0, 1)) = -1 Then
Return False
End If
If s.Bytes = 2 Then
If HexDigits.IndexOf(s.Middle(1, 1)) = -1 Then
Return False
End If
End If
Return True
Am I missing something with .FromHex?