大数十进制转二进制

时间:2021-12-11 03:38:53

'无限制大数十进制转二进制函数 

'调用举例:

'    Debug.Print BigDec_to_Bin("79228162514264337593543950335")
'    Debug.Print BigDec_to_Bin("7922816251426433759354395033579228162514264337593543950335")

'参考文章 http://blog.csdn.net/chinaboyzyq/archive/2010/03/22/5403914.aspx

Public Function BigDec_to_Bin(ByVal Dec As String) As String
    Dim i As Long
    'Dim strDec As String
    Dim strRet As String
    Dim strMod As String
    Do
        strRet = "": strMod = ""
        BigDiv2 Dec, strRet, strMod
        Dec = strRet
        BigDec_to_Bin = strMod & BigDec_to_Bin
        DoEvents
    Loop Until strRet = "0"
End Function

Private Sub BigDiv2(ByVal Dec As String, strRet As String, strMod As String)
    Dim i As Long
    Dim strTmp As String
    strMod = 0
    For i = 1 To Len(Dec)
        strRet = strRet & (Val(Mid(Dec, i, 1)) + strMod * 10) / 2
        strMod = (Val(Mid(Dec, i, 1)) + strMod * 10) Mod 2
    Next
    If Left(strRet, 1) = "0" And Len(strRet) > 1 Then strRet = Right(strRet, Len(strRet) - 1)
End Sub