代码如下: dim i as long,l as long,out as long dim str1 as string l=len(text1.text) for i=1 to l str1=Ucase(mid(text1.text,i,1)) if "0"<=str1 and str1<="9" then out=out+16^(l-i)*clng(str1) elseif "A"<=str1 and str1<="F" then out=out+16^(l-i))*(asc(str1)-55) end if next便可得到text1的十进制之值
谢谢楼上的答复.我后来参考一本书的代码,用如下方法解决,写的比较粗糙,输入十六进制时,字节之间不能有空格. Public Function convertHexChr(str As String) As Integer Dim test As Integer test = Asc(str) If test >= Asc("0") And test <= Asc(9) Then test = test - Asc("0") ElseIf test >= Asc("a") And test <= Asc("f") Then test = test - Asc("a") + 10 ElseIf test >= Asc("A") And test <= Asc("F") Then test = test - Asc("A") - 10 Else test = -1 End If convertHexChr = test End FunctionPublic Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer Dim HexData As Integer Dim hstr As String * 1 Dim lstr As String * 1 Dim HighHexData As Integer Dim LowHexData As Integer Dim HexDataLen As Integer Dim StringLen As Integer Dim Account As IntegerDim n As Integer HexDataLen = 0 strHexToByteArray = 0StringLen = Len(strText) Account = StringLen / 2 ReDim bytByte(Account) strText = Trim(strText)For n = 1 To StringLen hstr = Mid(strText, n, 1) n = n + 1 lstr = Mid(strText, n, 1)
dim i as long,l as long,out as long
dim str1 as string
l=len(text1.text)
for i=1 to l
str1=Ucase(mid(text1.text,i,1))
if "0"<=str1 and str1<="9" then
out=out+16^(l-i)*clng(str1)
elseif "A"<=str1 and str1<="F" then
out=out+16^(l-i))*(asc(str1)-55)
end if
next便可得到text1的十进制之值
Public Function convertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc(9) Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") - 10
Else
test = -1
End If
convertHexChr = test
End FunctionPublic Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer
Dim hstr As String * 1
Dim lstr As String * 1
Dim HighHexData As Integer
Dim LowHexData As Integer
Dim HexDataLen As Integer
Dim StringLen As Integer
Dim Account As IntegerDim n As Integer
HexDataLen = 0
strHexToByteArray = 0StringLen = Len(strText)
Account = StringLen / 2
ReDim bytByte(Account)
strText = Trim(strText)For n = 1 To StringLen
hstr = Mid(strText, n, 1)
n = n + 1
lstr = Mid(strText, n, 1)
HighHexData = convertHexChr(hstr)
LowHexData = convertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next nIf HexDataLen > 0 Then
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End IfIf StringLen = 0 Then
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function