|
任意进制转换的自定义函数:
x为转换对象数值,n为将要转化的进制,p为转换前进制(默认10进制),d为转换输出数位- Function dec2base(x, n, Optional p = 10, Optional d = 0)
- If p = 0 Then p = 10
- If p = 10 And (x >= 2 ^ 31 Or x <= -2 ^ 31) Then Exit Function
- If n < 2 Or n > 36 Or p < 2 Or p > 36 Then Exit Function
-
- If x < 0 Then x = x + 2 ^ 31: k = -1
- If p < 10 Then
- For i = Len(x) To 1 Step -1
- y = y + Mid(x, i, 1) * p ^ (Len(x) - i)
- Next
- ElseIf p = 10 Then
- y = x
- ElseIf p > 10 Then
- For i = Len(x) To 1 Step -1
- y = y + IIf(IsNumeric(Mid(x, i, 1)), Mid(x, i, 1), Asc(Mid(x, i, 1)) - 55) * p ^ (Len(x) - i)
- Next
- End If
- If k = -1 Then
- For i = 1 To d
- dec2base = IIf(y Mod n < 10, y Mod n, Chr(y Mod n + 55)) & dec2base
- y = Int(y / n)
- Next
- Else
- Do Until y = 0
- dec2base = IIf(y Mod n < 10, y Mod n, Chr(y Mod n + 55)) & dec2base
- y = Int(y / n)
- Loop
- If Len(dec2base) < d Then dec2base = Right(String(d, "0") & dec2base, d)
- End If
-
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|