Decimal to binary conversion for large numbers in Excel

后端 未结 12 2132
长情又很酷
长情又很酷 2020-12-14 02:31

I have some large numbers in an Excel sheet and I want to convert them to binary.

e.g.

12345678  
965321458  
-12457896
12条回答
  •  孤城傲影
    2020-12-14 03:04

    This vba function solves the problem of binary conversion of numbers greater than 511 that can not be done with WorksheetFunction.dec2bin.
    The code takes advantage of the WorksheetFunction.dec2bin function by applying it in pieces.

    Function decimal2binary(ByVal decimal2convert As Long) As String
    Dim rest As Long
    If decimal2convert = 0 Then
       decimal2binary = "0"
       Exit Function
    End If
    Do While decimal2convert > 0
       rest = decimal2convert Mod 512
       decimal2binary = Right("000000000" + WorksheetFunction.Dec2Bin(rest), 9) + decimal2binary
       decimal2convert = (decimal2convert - rest) / 512
    Loop
    decimal2binary = Abs(decimal2binary)
    End Function
    

提交回复
热议问题