新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim a As String, b As String
a = "79228162514264337593543950335" '这是VB能处理的最大整数Decimal类型24个F,
'计算器转换此数到16进制时已经溢出得16个F
b = DecToHex(a)
Debug.Print b
a = "18446744073709551615" '这个数属Decimal类型转换后16个F
b = DecToHex(a)
Debug.Print b
a = "4294967295" '这是C语言无符号长整的最大数8个F
b = DecToHex(a)
Debug.Print b
a = "65535" '这是C语言无符号整形最大数4个F
b = DecToHex(a)
Debug.Print b
a = "1234567890" '随便给个不出错的数,可以对照计算机器的值
b = DecToHex(a)
Debug.Print b
a = "1111111111a1111111111" '这是一个出错的例子
b = DecToHex(a)
Debug.Print b
End Sub
Private Function DecToHex(ByVal c As String) As String
'10进制转16进制函数
'24个F以下(含24个F)的数不会出错
On Error GoTo imsg
Dim a, b, i As Integer, m As Integer
Dim iH() As String, iHex As String
If c = "0" Then
DecToHex = 0
Exit Function
End If
a = CDec(c)
b = CDec(b)
While a > 0
ReDim Preserve iH(i)
b = Int(a / 16)
iH(i) = a - b * 16
a = b
i = i + 1
Wend
For m = i - 1 To 0 Step -1
iHex = iHex & Hex(iH(m))
Next
DecToHex = iHex
Exit Function
imsg:
Debug.Print "原数据错误!";
DecToHex = ""
End Function
Private Sub Command2_Click()
Debug.Print DecToHex("5368709120")
End Sub
Command1运行结果:
FFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFF
FFFF
499602D2
原数据错误!
Command2运行结果:
140000000