在 MS ACCESS VBA 中,我通过在字符串前加上“&”前缀将十六进制字符串转换为十进制
?CLng("&h1234")
4660
?CLng("&h80000000")
-2147483648
我应该怎么做才能将其转换为无符号整数?
使用 CDbl 也不起作用:
?CDbl("&h80000000")
-2147483648
您的版本似乎是最好的答案,但可以稍微缩短:
Function Hex2Dbl(h As String) As Double
Hex2Dbl = CDbl("&h0" & h) ' Overflow Error if more than 2 ^ 64
If Hex2Dbl < 0 Then Hex2Dbl = Hex2Dbl + 4294967296# ' 16 ^ 8 = 4294967296
End Function
Double
大多数大于 2 ^ 53 - 1 的值都会出现舍入精度误差(约16位小数 https://en.wikipedia.org/wiki/Double-precision_floating-point_format), but Decimal
可用于最大 16 ^ 12 - 1 (Decimal
使用 16 个字节,但仅使用其中的 12 个字节)
Function Hex2Dec(h)
Dim L As Long: L = Len(h)
If L < 16 Then ' CDec results in Overflow error for hex numbers above 16 ^ 8
Hex2Dec = CDec("&h0" & h)
If Hex2Dec < 0 Then Hex2Dec = Hex2Dec + 4294967296# ' 2 ^ 32
ElseIf L < 25 Then
Hex2Dec = Hex2Dec(Left$(h, L - 9)) * 68719476736# + CDec("&h" & Right$(h, 9)) ' 16 ^ 9 = 68719476736
End If
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)