Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DEC2BIN() with large numbers

I'm trying to convert 4503599627370495 into binary in Excel. DEC2BIN() returns #NUM! error because DEC2BIN cannot handle such a large number.

Any thoughts on how I might be able to make it work?

like image 330
user3367131 Avatar asked Mar 01 '14 01:03

user3367131


People also ask

What does DEC2BIN do in Matlab?

Description. str = dec2bin( d ) returns the binary representation of symbolic number d as a character vector. d must be a nonnegative integer. If d is a matrix or multidimensional array of symbolic numbers with N elements, dec2bin returns a character array with N rows.


2 Answers

This is super simple, Base(...) function can help you.

BASE(CELL, 2)

The second param 2 is for binary, you can convert to other relevant bases as Hex, Oct

like image 115
Alberto Saint Avatar answered Oct 05 '22 12:10

Alberto Saint


Thanx JustinDavies - that was just what I needed, but it went into an endless loop if passed a -ve number. My modification:

Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
  DecToBin = ""
  DecimalIn = CDec(DecimalIn)
  If DecimalIn < 0 Then
    DecToBin = "Error - Number negative"
    Exit Function
  End If
  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
like image 28
AndruWitta Avatar answered Oct 05 '22 10:10

AndruWitta