Decimal to binary conversion for large numbers in Excel

后端 未结 12 2083
长情又很酷
长情又很酷 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 02:52

    See VBA posted here

    ' The DecimalIn argument is limited to 79228162514264337593543950245
    ' (approximately 96-bits) - large numerical values must be entered
    ' as a String value to prevent conversion to scientific notation. Then
    ' optional NumberOfBits allows you to zero-fill the front of smaller
    ' values in order to return values up to a desired bit level.
    Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
      DecToBin = ""
      DecimalIn = CDec(DecimalIn)
      Do While DecimalIn <> 0
        DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
        DecimalIn = Int(DecimalIn / 2)
      Loop
      If Not IsMissing(NumberOfBits) Then
        If Len(DecToBin) > NumberOfBits Then
          DecToBin = "Error - Number too large for bit size"
        Else
          DecToBin = Right$(String$(NumberOfBits, "0") & _
          DecToBin, NumberOfBits)
        End If
      End If
    End Function
    
    0 讨论(0)
  • 2020-12-14 02:53

    There maybe a simple solution. I have several 4.2 billion cells that are actually a negative Two's Complement and this works to get the correct value: =SUM(2^31-(A1-2^31))

    0 讨论(0)
  • 2020-12-14 02:54

    Without VBA and working with negative numbers as well (here: sint16), however, taking much more space:

    You can download the excel file here: (sorry, didn't know where to put the file) int16 bits to decimal.xlsx

    or alternatively follow these steps (if your Excel is not in English, use Excel Translator to "translate" the formula into your MS Office language):

    1. Enter the binary number in 4-bit nibbles (A4 = most significant to D4 = least significant) like shown in the screenshot. Enter all 4 digits (even if starting with 0) and format them as "text"
    2. Enter formula in F4:
        =IF(NUMBERVALUE(A4)>=1000,TRUE,FALSE)
    
    1. Enter the letter "A" in G2-J2, "B" in K2-N2, "C" in O2-R2, "D" in S2-V2

    2. Enter "1" in G3, K3, O3 and S3; "2" in H3, L3, P3 and T3; "3" in I3, M3, Q3 and U3; "4" in J3, N3, R3 and V3

    3. In G4, enter:

        =MID(INDIRECT(G$2&ROW()),G$3,1)
    
    1. Copy the formula to H4-V4

    2. In X4, enter:

        =IF(G4="1",0,1)
    
    1. Copy X4 to Y4-AM4

    2. In BD3 enter "1"

    3. In BC4, enter:

        =IF((AM$4+BD3)=2,1,0)
    
    1. IN BD4, enter:
        =IF((AM$4+BD3)=2,0,IF((AM$4+BD3)=1,1,0))
    
    1. Copy BD4 and BD4 and insert it 15 times diagonally one row further down and one column further left (like in the screenshot), i.e. insert it to BB5 and BC5, then BA6 and BB6, ..., AN19 and AO19.

    2. In AO20, enter "=AO19"; in AP20, enter "=AP18" and so on until BD20 ("=BD4") - i.e. bring down the numbers into one line as seen in the screenshot

    3. In BE20, enter (this is your result):

        =IF(F4=FALSE,BIN2DEC(A4&B4)*2^8+BIN2DEC(C4&D4),-1*(BIN2DEC(AO20&AP20&AQ20&AR20&AS20&AT20&AU20&AV20)*2^8+BIN2DEC(AW20&AX20&AY20&AZ20&BA20&BB20&BC20&BD20)))
    
    0 讨论(0)
  • 2020-12-14 02:56

    I just tried the formula above, and found that Microsoft screwed up the DEC2BIN function in another way that keeps the formula from working correctly with negative numbers. Internally, DEC2BIN uses a ten bit result; leading zeroes are dropped from the text result, unless the optional length parameter is used, in which case the required number of leading zeroes are left in the string. But here's the rub: a negative number always starts with a one, so there are no leading zeroes to drop, so DEC2BIN will always show all ten bits! Thus, DEC2BIN(-1,8), which should show 11111111 (eight ones) will instead show 1111111111 (ten ones.)

    To fix this, use RIGHT to trim each eight bit chunk to eight bits, dumb as that sounds.

    =RIGHT(DEC2BIN(QUOTIENT(A1,256^3),8),8) & RIGHT(...
    

    (I read through the VBA, and it does not have the same problem, but it doesn't look like it will handle negatives at all.)

    0 讨论(0)
  • 2020-12-14 02:57

    Here's another way. It's not with a single formula, but I have tried and converted up to the number 2,099,999,999,999. My first intention was to build a 51 bit counter, but somehow it does not work with numbers beyond the one I mentioned. Download from http://www.excelexperto.com/content/macros-production/contador-binario-de-51-bits/

    I hope it's useful. Regards.

    0 讨论(0)
  • 2020-12-14 02:58

    While I didn't write this for negatives or decimals, it should be relatively easy to modify. This VBA will convert any super large (or not so large if you want, but that wasn't the point) decimal up to the converted binary result containing up to 32767 digits (maximum string length in VBA).

    Enter decimal in cell "A1" as a string, result will be in "B1" as a string.

    Dim NBN As String
    
    Dim Bin As String
    
    5 Big = Range("A1")
    
    AA = Len(Big)
    
    For XX = 1 To AA
    
    L1 = Mid(Big, XX, 1) + CRY
    
    CRY = 0
    
    If L1 = 0 Then
    
    FN = "0"
    
    GoTo 10
    
    End If
    
    If Int(L1 / 2) = L1 / 2 Then
    
    FN = L1 / 2
    
    GoTo 10
    
    End If
    
    If Int(L1 / 2) <> L1 / 2 Then
    
    FN = Int(L1 / 2)
    
    CRY = 10
    
    GoTo 10
    
    End If
    
    10 NBN = NBN & FN
    
    Next XX
    
    If Left(NBN, 1) = "0" Then
    
    NBN = Right(NBN, (Len(NBN) - 1))
    
    End If
    
    If CRY = 10 Then Bin = "1" & Bin Else Bin = "0" & Bin
    
    Range("A1") = NBN
    
    Range("A2") = Bin
    
    If Len(NBN) > 0 Then
    
    NBN = ""
    
    CRY = 0
    
    GoTo 5
    
    End If
    
    0 讨论(0)
提交回复
热议问题