VB 16进制转10进制的问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 18:54:51
代码如下:
Private Sub Command1_Click()
ST Text1
End Sub

Private Sub ST(x As String)
Dim s As Integer, a As Integer, i As Integer
For i = 1 To Len(x)
x = Mid(x, i, 1)
If Asc(x) >= 65 And Asc(x) <= 70 Then
a = Asc(x) - 55
If Asc(x) >= 97 And Asc(x) <= 102 Then
a = Asc(x) - 87
Else
a = x
End If
End If
If Len(x) - i = 0 Then
s = s + a
Else
s = s + a * 16 * (Len(x) - i)
End If
Next i
Text2 = Text2 & s
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 65 Or KeyAscii > 70) And (KeyAscii < 97 Or KeyAscii > 102) And (KeyAscii < 48 Or KeyAscii > 57) Then
i = MsgBox("请输入16进制数")
End If
End Sub
我的在TEXT1里面输字母结果就是0,输数字就提示ASC(X)错误呢?
我这里是单独算的16进制数转换10进制
请问算法上有什么不对吗?

Private Sub Command1_Click()
Text2.Text = ""
ST Text1.Text
End Sub

Private Sub ST(strz As String)
Dim s As Integer, a As Integer, i As Integer, strg As String 'strz是你输入的16进制数
For i = Len(strz) To 1 Step -1
strg = Mid(strz, i, 1)
If Asc(strg) >= 65 And Asc(strg) <= 70 Then
a = Asc(strg) - 55
ElseIf Asc(strg) >= 97 Then
a = Asc(strg) - 87
Else
a = strg
End If
s = s + a * 16 ^ (i - 1)
Next i
Text2 = Text2 & s
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 65 Or KeyAscii > 70) And (KeyAscii < 97 Or KeyAscii > 102) And (KeyAscii < 48 Or KeyAscii > 57) Then
i = MsgBox("请输入16进制数")
End If
End Sub
我也刚学VB``希望能帮上你``
你的代码里这句
s = s + a * 16 * (Len(x) - i)算法错误
该写成s=s+a*16^(len(x)-i)

至于么?

你在text1输入的16进制数想转换为10进制只需要<