[英]How can I check if a string contains an hexadecimal value in VBScript?
Is there a function in VBScript to check if a string contains a valid hexadecimal value? VBScript 中是否有 function 来检查字符串是否包含有效的十六进制值?
For Example:例如:
Dim x : x = IsHexadecimal("2AF3") 'x = True
Dim y : y = IsHexadecimal("X2M3") 'y = False
You could use a regular expression, as others have already suggested: 您可以使用正则表达式,正如其他人已经建议的那样:
Function IsHexadecimal(ByVal v)
Set re = New RegExp
re.Pattern = "^[a-f0-9]+$"
re.IgnoreCase = True
IsHexadecimal = re.Test(v)
End Function
Another solution (with a little more hack value): 另一种解决方案(具有更多黑客价值):
Function IsHexadecimal(ByVal v)
On Error Resume Next
Dim i : i = CLng("&h" & v)
IsHexadecimal = Not IsEmpty(i)
On Error Goto 0
End Function
I realise this is an old thread, but here is my solution. 我意识到这是一个老线程,但这是我的解决方案。 It is based on vbscript's built-in
IsNumeric
function that can identify a hex string provided it is preceded by &h
它基于vbscript的内置
IsNumeric
函数,可以识别十六进制字符串,前提是它以&h
function is_hex(str_hex)
is_hex = IsNumeric("&h" & str_hex)
end function
Not sure if such a function is built-in to vbscript, but you could roll your own with something like this: 不确定这样的函数是否内置于vbscript中,但你可以用这样的东西自己动手:
function isHexadecimal(s as string)
Dim i as integer, c as string, R as boolean
R=true
for i=1 to length(s)
c=mid(S,i,1)
if instr("0123456789ABCDEFabcdef", c)<=0 then R=false
next i
sHexadecimal=R
end function
You could use RegEx, like this: 您可以使用RegEx,如下所示:
'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "\A\b[0-9a-fA-F]+\b\Z"
' Pop up a message box for each match
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
msgbox myMatch.Value, 0, "Found Match"
Next
Check this link http://www.regular-expressions.info/vbscript.html 请查看此链接http://www.regular-expressions.info/vbscript.html
Hope it helps ^_^ 希望它有帮助^ _ ^
Same as Ansgar Wiechers' answer, but the regex needs to be af, not ae. 与Ansgar Wiechers的答案相同,但正则表达式需要af,而不是ae。
Function IsHexadecimal(ByVal v)
Set re = New RegExp
re.Pattern = "^[a-f0-9]+$"
re.IgnoreCase = True
IsHexadecimal = re.Test(v)
End Function
Either write a function that implements: 写一个实现的函数:
For Each character In input
IF character Is Not In "0-9A-Za-z"
Exit with False
End If
Next
Exit with True
or use a RegExp.Test() with the .Pattern "[^0-9A-Za-z]". 或者使用带有.Pattern“[^ 0-9A-Za-z]”的RegExp.Test()。
In both cases, checking the length of the input may be a good idea. 在这两种情况下,检查输入的长度可能是个好主意。
I wrote a specific function. 我写了一个特定的功能。
The following code works fine: 以下代码工作正常:
Function IsHexadecimal(ByVal input_string)
Dim b : b = False
Dim s_length : s_length = Len(input_string)
Dim char
For i = 1 To s_length
char = Asc(Mid(input_string, i, 1))
If (char > 47 And char < 58) Or _
(char > 64 And char < 71) Or _
(char > 96 And char < 103) Then
b = True
Else
b = False
Exit For
End If
Next
IsHexadecimal = b
End Function
Maybe not the most optimized but it is easy and it works fairly well:也许不是最优化的,但它很容易并且效果很好:
Private Function IsHexadecimal(Text2Verify As String) As Boolean
Dim i As Integer
For i = 1 To Len(Text2Verify)
If Not Mid(Text2Verify, i, 1) Like "*[*0*1*2*3*4*5*6*7*8*9*A*B*C*D*E*F*a*b*c*d*e*f*]*" Then
IsHexadecimal = False
Exit Function
End If
Next i
IsHexadecimal = True
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.