For general integer numbers, 16-based and 10-based conversion can be solved with clng (), HEX () function, but encounter astronomical numbers, these functions are powerless. Below is a few functions written by the author, demonstrate some techniques in astronomical digital calculations.
DimlarHex As String, LargeDec As String, Start As Long, Y (20) AS STRING
'Preparation function function sus (byval x as string, byval y as string) AS string' Sum of Two hugehexnum (two large numbers) 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 (two large amounts) 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 Integer I = 1 To Xpowers = Multi (Powers, ClNG & H1000000)) NEXTEND FUNCTIONFUNCTION HALF (BYVAL X AS STRING) AS STRING 'GET HALF OF X (Take Half) 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 (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' Another useful 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
'Enter Conversion Function:
'16 to 10Function Hextodec (BYVAL X AS STRING) AS STRINGDIM A () AS STRING, I as Long, Unit As INTEGER I = 1 to Len (x) ife = 1 to Len (x) ife = 1 To Len (x) IF Not ISNUMERIC ("& H" & MID (x, i, 1) "NOT A HEX FORMAT!", 64, "info": exit function 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 (HEXTEC, 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) NextLoPDectoHex = HEX (Val (hugenum)) & DectoHexend FunctionPrivate 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) & "bits):" & largehex & vbCrLf & vbCrLf & "dec (" & Len (largedec) & "Bit):" & LargeDec, 64, "Time" & format ((Timer - start), "0.0000") & "Second!" End Sub
'DectohexPrivate Sub Command2_Click () largedec = "27305594525408320787401222904174795936368587913861811995606068514338921239280447480038845811151419865392100570221250636783105942723266982313358992551204806603060637911792055430458953651997903849585424629638958641829173494438455892966522070157613386886352421847833413821003678138295449221439062614172249927946884678471687751616589458280098503446100701588657220466765694306218356144887228155732857434394095"
START = TIMERLARGEHEX = DECTOHEX (LargeDec) MSGBox "DEC (" & Len (LargeDec) & "Bits):" & LargeDec & Vbcrf & Vbcrf & "HEX (" & Len (LargeHex) & "Bits):" & LargeHex, 64 , "Time" & Format ((Timer - Start), "0.0000") & "Second!" End Sub