关于天文数字十进制与十六进制间的转换 对于一般的整型数字,16进制与10进制 间的转化可以用CLNG(),HEX()函数解决,但遇上天文数字,这些函数就无能为力了。下面是笔者写的几个函数,演示了天文数字计算中的一些技巧。 Dim largehex As String, largedec As String, start As Long, Y(20) As String '预备函数Function sums(ByVal X As String, ByVal Y As String) As String ' sum of two hugehexnum(两个大数之和)Dim max As Long, temp As Long, I As Long, result As Variantmax = IIf(Len(X) >= Len(Y), Len(X), Len(Y))X = Right(String(max, "0") & X, max)Y = Right(String(max, "0") & Y, max)ReDim result(0 To max)For I = max To 1 Step -1result(I) = Val(Mid(X, I, 1)) + Val(Mid(Y, I, 1))NextFor I = max To 1 Step -1temp = result(I) \ 10result(I) = result(I) Mod 10result(I - 1) = result(I - 1) + tempNextIf result(0) = 0 Then result(0) = ""sums = Join(result, "")Erase result End Function Function multi(ByVal X As String, ByVal Y As String) As String 'multi of two huge hexnum(两个大数之积)Dim result As VariantDim xl As Long, yl As Long, temp As Long, I As Longxl = Len(Trim(X))yl = Len(Trim(Y)) ReDim result(1 To xl + yl)For I = 1 To xlFor temp = 1 To ylresult(I + temp) = result(I + temp) + Val(Mid(X, I, 1)) * Val(Mid(Y, temp, 1))NextNext For I = xl + yl To 2 Step -1temp = result(I) \ 10result(I) = result(I) Mod 10result(I - 1) = result(I - 1) + tempNext If result(1) = "0" Then result(1) = ""multi = Join(result, "")Erase result End FunctionFunction POWERS(ByVal X As Integer) As String ' GET 16777216^X,ie 16^(6*x)(16777216的X 次方)POWERS = 1Dim I As IntegerFor I = 1 To XPOWERS = multi(POWERS, CLng(&H1000000))NextEnd FunctionFunction half(ByVal X As String) As String 'get half of x(取半)X = 0 & XDim I As LongReDim result(2 To Len(X)) As StringFor I = 2 To Len(X)result(I) = CStr(Val(Mid(X, I, 1)) \ 2 + IIf(Val(Mid(X, I - 1, 1)) Mod 2 = 1, 5, 0))Nexthalf = Join(result, "")If Left(half, 1) = "0" Then half = Right(half, Len(half) - 1) ' no zero aheadEnd Function '另一个有用的函数:Function POWERXY(ByVal X As Integer, ByVal Y As Integer) As String 'GET X^Y(X 的 Y 次方)Dim I As IntegerPOWERXY = XFor I = 2 To YPOWERXY = multi(POWERXY, X)NextEnd Function '进制转换函数: '16 to 10Function HEXTODEC(ByVal X As String) As StringDim A() As String, I As Long, UNIT As IntegerFor I = 1 To Len(X)If Not IsNumeric("&h" & Mid(X, I, 1)) Then MsgBox "NOT A HEX FORMAT!", 64, "INFO": Exit FunctionNextX = String((6 - Len(X) Mod 6) Mod 6, "0") & X UNIT = Len(X) \ 6 - 1ReDim A(UNIT)For I = 0 To UNITA(I) = CLng("&h" & Mid(X, I * 6 + 1, 6))NextFor I = 0 To UNITA(I) = multi(A(I), POWERS(UNIT - I))HEXTODEC = sums(HEXTODEC, A(I))NextEnd Function ' 10 to 16Function dectohex(ByVal hugenum As String) As String ' trans hugenum to hexDo While Len(hugenum) > 2dectohex = Hex(Val(Right(hugenum, 4)) Mod 16) & dectohexFor I = 1 To 4 'devide hugenum by 16hugenum = half(hugenum)NextLoopdectohex = Hex(Val(hugenum)) & dectohexEnd Function Private Sub Form_Load()For I = 0 To 20Y(I) = "1234567890ABCDEF"Next largehex = Join(Y, "")End Sub 'hextodecPrivate Sub Command1_Click()start = Timerlargedec = HEXTODEC(largehex)Debug.Print largedecMsgBox "hex(" & Len(largehex) & " 位): " & largehex & vbCrLf & vbCrLf & "dec(" & Len(largedec) & " 位): " & largedec, 64, "用时" & Format((Timer - start), "0.0000") & " 秒!"End Sub 'dectohexPrivate Sub Command2_Click()largedec = "27305594525408320787401222904174795936368587913861811995606068514338921239280447480038845811151419865392100570221250636783105942723266982313358992551204806603060637911792055430458953651997903849585424629638958641829173494438455892966522070157613386886352421847833413821003678138295449221439062614172249927946884678471687751616589458280098503446100701588657220466765694306218356144887228155732857434394095" start = Timerlargehex = dectohex(largedec)MsgBox "dec(" & Len(largedec) & " 位): " & largedec & vbCrLf & vbCrLf & "hex(" & Len(largehex) & " 位): " & largehex, 64, "用时" & Format((Timer - start), "0.0000") & " 秒!"End Sub 'get x^yPrivate Sub Command3_Click()start = TimerMsgBox "2^3000=" & POWERXY(2, 3000), 64, "用时" & Format((Timer - start), "0.0000") & " 秒!"End Sub

评论