我正在尝试将十进制数转换为十六进制数,内置的十六进制函数限制为8个字符,因此我必须编写自己的函数,但是VBScript似乎将数字四舍五入到小数点后两位。
例如。
106681252129194/16 = 6667578258074.625
问题是当我在VBScript中执行此代码时
strResult = 106681252129194 / 16
结果是6667578258074.63,这使得计算十六进制数字成为一个小问题。
答案 0 :(得分:1)
你必须做一个整数除法和余数运算。被邀请查看我尝试的数字(双倍< 2 ^ 50)到ascii函数:
Function ntoa( nNum, iBase )
ntoa = "0"
If nNum Then
ntoa = Mid( "-", Sgn( nNum ) + 2 ) + ntoaPos( Abs( nNum ), iBase )
End If
End Function
Function ntoaPos( nNum, iBase )
If nNum >= 1 Then
Dim nD : nD = Fix( nNum / iBase )
Dim nM : nM = nNum - nD * iBase
ntoaPos = ntoaPos( nD, iBase ) _
& Mid( "0123456789ABCDEFGHIJKLMNOPQRSTUV", 1 + nM, 1 )
End If
End Function
基本测试代码:
Dim aTests : aTests = Array( _
"zero (default: long)", 0 _
, "zero (Byte)" , CByte( 0 ) _
, "min_long" , CLng( -2^31 ) _
, "max_long" , CLng( 2^31-1 ) _
, "min_long - 1" , -2^31-1 _
, "max_long + 1" , 2^31 _
, "max_ulong" , 4294967295 _
, "big" , 2^40 _
, "bigger" , 2^45 _
, "bigger-1" , 2^45-1 _
, "bigger-11" , 2^45-11 _
, "very big" , 2^50 _
)
Dim nTest
For nTest = 0 To UBound( aTests ) Step 2
WScript.Echo "-----", nTest \ 2, aTests( nTest )
Dim nVal : nVal = aTests( nTest + 1 )
WScript.Echo " Type:", TypeName( nVal ), VarType( nVal )
WScript.Echo " Value:", nVal
WScript.Echo " hexxed:", ntoa( nVal, 16 )
WScript.Echo " decced:", ntoa( nVal, 10 )
WScript.Echo " abced:", ntoa( nVal, 32 )
Next
输出:
----- 0 zero (default: long)
Type: Integer 2
Value: 0
hexxed: 0
decced: 0
abced: 0
----- 1 zero (Byte)
Type: Byte 17
Value: 0
hexxed: 0
decced: 0
abced: 0
----- 2 min_long
Type: Long 3
Value: -2147483648
hexxed: -80000000
decced: -2147483648
abced: -2000000
----- 3 max_long
Type: Long 3
Value: 2147483647
hexxed: 7FFFFFFF
decced: 2147483647
abced: 1VVVVVV
----- 4 min_long - 1
Type: Double 5
Value: -2147483649
hexxed: -80000001
decced: -2147483649
abced: -2000001
----- 5 max_long + 1
Type: Double 5
Value: 2147483648
hexxed: 80000000
decced: 2147483648
abced: 2000000
----- 6 max_ulong
Type: Double 5
Value: 4294967295
hexxed: FFFFFFFF
decced: 4294967295
abced: 3VVVVVV
----- 7 big
Type: Double 5
Value: 1099511627776
hexxed: 10000000000
decced: 1099511627776
abced: 100000000
----- 8 bigger
Type: Double 5
Value: 35184372088832
hexxed: 200000000000
decced: 35184372088832
abced: 1000000000
----- 9 bigger-1
Type: Double 5
Value: 35184372088831
hexxed: 1FFFFFFFFFFF
decced: 35184372088831
abced: VVVVVVVVV
----- 10 bigger-11
Type: Double 5
Value: 35184372088821
hexxed: 1FFFFFFFFFF5
decced: 35184372088821
abced: VVVVVVVVL
----- 11 very big
Type: Double 5
Value: 1,12589990684262E+15
hexxed: 4000000000000
decced: 1125899906842624
abced: 10000000000
顺便说一句 - 你不能使用\:
>> a = 2 ^ 32
>> WScript.Echo a
>> b = a / 2
>> WScript.Echo b
>> c = a \ 2
>> WScript.Echo c
>>
4294967296
2147483648
Error Number: 6
Error Description: Overflow
答案 1 :(得分:0)
您超出了类型Double
提供的精度,即8个字节。
分组自动将事物自动舍入到小数点后两位;只是你的第3个+小数位无处可去,因此被淘汰了。
一种解决方案是使用类型Decimal
,它提供14个字节的存储空间。
Dim r As Decimal
r = 106681252129194) / 16 ' r = 6667578258074.625
注意:以上是针对VB的。在VBA和VBScript中,Decimal
类型只能在Variant
内访问。您不能直接声明Decimal
类型的变量;您必须创建Variant
,然后使用Decimal
函数创建其子类型CDec
:
Dim r
r = CDec(106681252129194) / CDec(16) ' r = 6667578258074.625
谨慎谨慎...... Decimal
会给你28-29个重要数据,而Double
只给你14-15个。另一方面,Double
为你提供了在-324和+308之间的10的幂的灵活性,如科学记数法,而Decimal
你用直线符号,没有10的幂。