一个更好的Excel Bin2Dec函数(签名)?

时间:2011-02-06 00:47:40

标签: excel vba excel-vba

我想知道是否有人为Excel编写了比包含的更好的Bin2Dec VBA宏。我正在寻找一个可以采用任意大二进制数(在~16位内)的函数,并使用有符号位。 Excel的BIN2DEC函数在12位后溢出,它通常非常难看(Excel不太好,因为数字也快速溢出)。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

写了一个:

Function Bin2SignedDec(sMyBin As String) As Long
    Dim x As Integer
    Dim iLen As Integer
    Dim sign As Boolean
    Dim tmp As String

    sign = Mid(sMyBin, 1, 1) = "1"


    iLen = Len(sMyBin) - 1
    For x = 0 To iLen
        tmp = Mid(sMyBin, iLen - x + 1, 1)
        If sign Then
            If tmp = "1" Then
                tmp = "0"
            Else
                tmp = "1"
            End If
        End If
        Bin2SignedDec = Bin2SignedDec + _
          tmp * 2 ^ x
    Next
    If sign Then
        Bin2SignedDec = (Bin2SignedDec * -1) - 1
    End If

End Function

This link got me started