使用公式将十进制转换为三十六进制

5

在Excel中,有没有一种简单的方法将数字从十进制转换为三十六进制?我有一个需要转换的超过2,000个数字列表,所以我不能在在线转换器中一个一个地进行转换。

我在想,即使有数学方法,我也可以使用公式。

2个回答

4

更好的解决方案是使用BASE函数

=Base(需要转换的数字,进制)

例如

=base(35,36) = Z

=base(36,36) = 10


第二个答案通常是最好的! - Chris
这对于旧版本的Office不起作用。BASE函数(以及它的对应函数DECIMAL)是在Excel 2013中引入的,不可用于早期版本的Excel。 - O.M.Y.

1

Have a look at this:

http://www.thetropicalevents.com/Xnumbers60/
http://www.thetropicalevents.com/Xnumbers60.htm

【翻译】

[按照Korem的要求添加了代码]

或者

Sub main()
    Dim base10Number As Double
    base10Number = Int(Rnd * 1000)
    Debug.Print base10Number, ConvertBase10(base10Number, "0123456789ABCDEF")
End Sub

Public Function ConvertBase10(ByVal d As Double, ByVal sNewBaseDigits As String) As String
    Dim S As String, tmp As Double, i As Integer, lastI As Integer
    Dim BaseSize As Integer
    BaseSize = Len(sNewBaseDigits)
    Do While Val(d) <> 0
        tmp = d
        i = 0
        Do While tmp >= BaseSize
            i = i + 1
            tmp = tmp / BaseSize
        Loop
        If i <> lastI - 1 And lastI <> 0 Then S = S & String(lastI - i - 1, Left(sNewBaseDigits, 1)) 'get the zero digits inside the number
        tmp = Int(tmp) 'truncate decimals
        S = S + Mid(sNewBaseDigits, tmp + 1, 1)
        d = d - tmp * (BaseSize ^ i)
        lastI = i
    Loop
    S = S & String(i, Left(sNewBaseDigits, 1)) 'get the zero digits at the end of the number
    ConvertBase10 = S
End Function

http://www.freevbcode.com/ShowCode.asp?ID=6604复制而来

或类似的内容...

=ConvertBase10(A1,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

Sub main()
     Dim MyNumber As Double
     MyNumber = 999999999999999#
     MsgBox MyNumber & ": " & ConvertBase10(MyNumber, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
End Sub

Public Function ConvertBase10(ByVal d As Double, ByVal sNewBaseDigits As String) As String
     Dim S As String, tmp As Double, i As Integer, lastI As Integer
     Dim BaseSize As Integer
     BaseSize = Len(sNewBaseDigits)
     Do While Val(d) <> 0
         tmp = d
         i = 0
         Do While tmp >= BaseSize
             i = i + 1
             tmp = tmp / BaseSize
         Loop
         If i <> lastI - 1 And lastI <> 0 Then S = S & String(lastI - i - 1, Left(sNewBaseDigits, 1)) 'get the zero digits inside the number
         tmp = Int(tmp) 'truncate decimals
         S = S + Mid(sNewBaseDigits, tmp + 1, 1)
         d = d - tmp * (BaseSize ^ i)
         lastI = i
     Loop
     S = S & String(i, Left(sNewBaseDigits, 1)) 'get the zero digits at the end of the number
     ConvertBase10 = S
End Function

从以下链接复制:https://groups.google.com/forum/?fromgroups=#!topic/microsoft.public.excel.worksheet.functions/yY7U_kX_FwU


1
虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。 - Korem
感谢您的反馈,Korem。 - Robert Ilbrink

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接