这是我写的程序.你可以慢慢看看.
Option Explicit
Private Sub Command1_Click()
Dim inputData As String
inputData = InputBox("请输入你的汉字")
Dim hexData As String
hexData = Hex(Asc(inputData))
Print inputData & "的十六进制数是:" & hexData
Print "现在使用十六进制数:" & hexData & "还原成汉字"
Print "你的十六进制数是:" & hexData & ",对应的汉字是:" & Chr(hexToDec(hexData))
End Sub
Private Function hexToDec(inputData As String) As Double
Dim i As Integer
Dim decOut As Double '转换为十进制数最后返回的数
Dim lengthHex As Integer '十六进制数的长度
Dim hexStep As Double
decOut = 0
inputData = UCase(inputData)
lengthHex = Len(inputData) '得到十六进制数的长度
For i = 1 To lengthHex '判断该数是否为16进制数
If IsNumeric(Mid(inputData, i, 1)) Or Mid(inputData, i, 1) = "A" Or Mid(inputData, i, 1) = "B" Or _
Mid(inputData, i, 1) = "C" Or Mid(inputData, i, 1) = "D" Or Mid(inputData, i, 1) = "E" Or _
Mid(inputData, i, 1) = "F" Then
'跳过
Else
MsgBox "不是一个十六进制数"
Exit Function
End If
Next
hexStep = 0
'转换为十进制数
For i = lengthHex To 1 Step -1
hexStep = hexStep * 16
If hexStep = 0 Then
hexStep = 1
End If
If Mid(inputData, i, 1) = "0" Then
decOut = decOut + (0 * hexStep)
ElseIf Mid(inputData, i, 1) = "1" Then
decOut = decOut + (1 * hexStep)
ElseIf Mid(inputData, i, 1) = "2" Then
decOut = decOut + (2 * hexStep)
ElseIf Mid(inputData, i, 1) = "3" Then
decOut = decOut + (3 * hexStep)
ElseIf Mid(inputData, i, 1) = "4" Then
decOut = decOut + (4 * hexStep)
ElseIf Mid(inputData, i, 1) = "5" Then
decOut = decOut + (5 * hexStep)
ElseIf Mid(inputData, i, 1) = "6" Then
decOut = decOut + (6 * hexStep)
ElseIf Mid(inputData, i, 1) = "7" Then
decOut = decOut + (7 * hexStep)
ElseIf Mid(inputData, i, 1) = "8" Then
decOut = decOut + (8 * hexStep)
ElseIf Mid(inputData, i, 1) = "9" Then
decOut = decOut + (9 * hexStep)
ElseIf Mid(inputData, i, 1) = "A" Then
decOut = decOut + (10 * hexStep)
ElseIf Mid(inputData, i, 1) = "B" Then
decOut = decOut + (11 * hexStep)
ElseIf Mid(inputData, i, 1) = "C" Then
decOut = decOut + (12 * hexStep)
ElseIf Mid(inputData, i, 1) = "D" Then
decOut = decOut + (13 * hexStep)
ElseIf Mid(inputData, i, 1) = "E" Then
decOut = decOut + (14 * hexStep)
ElseIf Mid(inputData, i, 1) = "F" Then
decOut = decOut + (15 * hexStep)
End If
Next
hexToDec = decOut
End Function