高手指点一下
Option Explicit
Private Sub Command1_Click()
Dim strData As String
strData = InputBox("请输入16进制数")
Print Hex2Dec(strData)
End Sub
Private Function Hex2Dec(InputData As String) As Double
Dim i As Integer
Dim decOut As Double
Dim LenHex As Integer
Dim HexStep As Double
Dim MidData As String
decOut = 0
InputData = UCase(InputData)
LenHex = Len(InputData)
For i = 1 To LenHex
MidData = Mid(InputData, i, 1)
If Not (IsNumeric(MidData) Or MidData = "A" Or MidData = "B" _
Or MidData = "C" Or MidData = "D" Or MidData = "E" Or MidData = "F") Then
MsgBox "不是合法的十六制进数!请重新输入.", vbExclamation, "数据错误"
Exit Function
End If
Next
HexStep = 0
For i = LenHex To 1 Step -1
HexStep = HexStep * 16
If HexStep = 0 Then HexStep = 1
MidData = Mid(InputData, i, 1)
If MidData = "0" Then
decOut = decOut + (0 * HexStep)
ElseIf MidData = "1" Then
decOut = decOut + (1 * HexStep)
ElseIf MidData = "2" Then
decOut = decOut + (2 * HexStep)
ElseIf MidData = "3" Then
decOut = decOut + (3 * HexStep)
ElseIf MidData = "4" Then
decOut = decOut + (4 * HexStep)
ElseIf MidData = "5" Then
decOut = decOut + (5 * HexStep)
ElseIf MidData = "6" Then
decOut = decOut + (6 * HexStep)
ElseIf MidData = "7" Then
decOut = decOut + (7 * HexStep)
ElseIf MidData = "8" Then
decOut = decOut + (8 * HexStep)
ElseIf MidData = "9" Then
decOut = decOut + (9 * HexStep)
ElseIf MidData = "A" Then
decOut = decOut + (10 * HexStep)
ElseIf MidData = "B" Then
decOut = decOut + (11 * HexStep)
ElseIf MidData = "C" Then
decOut = decOut + (12 * HexStep)
ElseIf MidData = "D" Then
decOut = decOut + (13 * HexStep)
ElseIf MidData = "E" Then
decOut = decOut + (14 * HexStep)
ElseIf MidData = "F" Then
decOut = decOut + (15 * HexStep)
End If
Next
Hex2Dec = decOut
End Function
Option Explicit
Private Sub Command1_Click()
Dim strData As String
strData = Inputbox("请输入16进制数")
Print Hex2Dec(strData)
End Sub
Private Function Hex2Dec(InputData As String) As Double
Dim i As Integer
Dim decOut As Double
Dim LenHex As Integer
Dim HexStep As Double
Dim MidData As String
decOut = 0
'InputData = UCase(InputData)
LenHex = Len(InputData)
' For i = 1 To LenHex
' MidData = Mid(InputData, i, 1)
' If Not (IsNumeric(MidData) Or MidData = "A" Or MidData = "B" _
' Or MidData = "C" Or MidData = "D" Or MidData = "E" Or MidData = "F") Then
' MsgBox "不是合法的十六制进数!请重新输入.", vbExclamation, "数据错误"
' Exit Function
' End If
' Next
HexStep = 1
For i = LenHex - 1 To 1 Step -1
HexStep = HexStep * 16
'If HexStep = 0 Then HexStep = 1
MidData = Mid(InputData, i, 1)
decOut = decOut + (Val("&H" & MidData) * HexStep)
Next
Hex2Dec = Val("&H" & Right(InputData, 1)) + decOut
End Function
你的貌似不错,我的那个没你的这个转的长~~