自己写的:
Private Sub Command1_Click()
Dim s As String
Dim j As Long
'如果是数字,则继续
If IsNumeric(Text1.Text) Then
j = CLng(Text1.Text)
'取数,存在溢出可能
s = Hex(j)
'转16进制
'补零
If Len(s) < 8 Then
s = String(8 - Len(s), "0") & s
End If
'每隔2位增加一个空格
s = Left(s, 2) & " " & Mid(s, 3, 2) & " " & Mid(s, 5, 2) & " " & Right(s, 2)
Text2.Text = s
'显示结果
End If
End Sub
---------------------------------------
拿 zhuyongxing 的代码改的
Dim tempcardnum As Long
Dim s As String
Dim s2 As String
tempcardnum = CLng(Text1.Text)
'增加处理函数,显式转换函数,此处有溢出
s = Hex(tempcardnum)
Select Case Len(s) '使用多分支选择,可以避免多次计算 len(s) 的值
Case 8
s2 = Mid(s, 1, 2) + " " + Mid(s, 3, 2) + " " + Mid(s, 5, 2) + " " + Mid(s, 7, 2)
Case 7
s2 = "0" + Mid(s, 1, 1) + " " + Mid(s, 2, 2) + " " + Mid(s, 4, 2) + " " + Mid(s, 6, 2)
Case 6
s2 = "00" + " " + Mid(s, 1, 2) + " " + Mid(s, 3, 2) + " " + Mid(s, 5, 2)
Case 5
s2 = "00" + " " + "0" + Mid(s, 1, 1) + " " + Mid(s, 2, 2) + " " + Mid(s, 4, 2)
Case 4
s2 = "00" + " " + "00" + " " + Mid(s, 1, 2) + " " + Mid(s, 3, 2)
Case 3
s2 = "00" + " " + "00" + " " + "0" + Mid(s, 1, 1) + " " + Mid(s, 2, 2)
Case 2
s2 = "00" + " " + "00" + " " + "00" + " " + s
Case 1
s2 = "00" + " " + "00" + " " + "00" + " " + "0" + s
Case Else
s2 = "00 00 00 00"
End Select
Text2.Text = s2