Question

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?

No correct solution

OTHER TIPS

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

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

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
=DEC2BIN(MOD(QUOTIENT($A$1,256^3),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^2),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^1),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^0),256),8)

courtesy of Taosique who answered the duplicate Decimal to binary conversion for large numbers in Excel .

I needed a VBA function to convert Excel decimal integers to binary since MS failed me, yet again. I came up with the following, but I had to accept a string of ones & zeros as output, which was OK for me. It uses log base 2 which may be unique (or not?) and works for all positive integers as-is.

Function Dec_Bin(dx As Integer) As String
    Dim x As Integer
    Dim y As Long
    Dim z As Integer
    Dim zz As Double
    Dim ch As String
    Dim str As String, s1 As String
    Dim lead As Boolean

    ch = String(15, "0")
'    Stop
    zz = Log(dx) / Log(2)
    z = Fix(zz)
    y = dx - 2 ^ z
    z = 15 - z
    Mid(ch, z, 1) = "1"
    While y > 0
        zz = Log(y) / Log(2)
        z = Fix(zz)
        y = y - 2 ^ z
        z = 15 - z
        Mid(ch, z, 1) = "1"
        Wend

    ch = ch & "B"
    Dec_Bin = ch
End Function

This function will convert as big as a Double can hold. I didn't try it with negative values, though.

Function cn(ByVal n As Double, ByVal s As Double)
  'n the number to convert
  's the numberic system to convert to.
  'This function can convert to binary all the way to the length of the
  'digits string and all in between.

  Dim x As Double  'The exponent without decimals
  Dim xx As Double 'The exponent with decimals, if any
  Dim r As String  'The return string
  Dim p As Integer 'Posistion of the digit in the return string
  Dim L As Long    'Length of the string return string
  Dim d            '(d+1) because mid() does not accept 0.
                   'The position of the digit in the digits string.
 Dim v As Double   'The numeric value of the position 
                   'of the digit in the return string
  Dim digits As String
  digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Start:

  If n > 0 Then
     xx = Log(n) / Log(s)
     x = Int(xx)
  End If
  p = x + 1
  If r = "" Then
    r = String(p, "0")
    L = p
  End If
  v = s ^ x
  d = n \ v
  Mid(r, L - x, 1) = Mid(digits, d + 1, 1)
  n = n - (v * d)
  If n <> 0 Then GoTo Start
  cn = r
End Function

Normally when I need to know the binary of a decimal value, it is always interesting to see the hexadecimal value. The function does not use MOD or HEX() for the big number since Microsoft limited it, so it is done manually for very large numbers.

Bino(Value,Bytes)

Value can be any integer decimal number or a cell contained it, small or superbig. Bytes is optional, standard 2, automatic.

=Bino(A1,4)

The number or bytes and bits presented is automatic based on its value, standard minimum of 2 bytes, but you can pad with extra zero bytes at left, using the optional bytes argument. The example above will show A1 with 4 bytes and 32 bits, even if A1 contains a single numeric digit, or will use more bytes and bits if larger than 4.

=Bino(A1)

Will show A1 with a minimum of 2 bytes and 16 bits, or larger if necessary. For better visualization, binary format has nibbles separated by "-" and bytes by "|"

=Bino(129) = 0x0081 = 0000-0000|1000-0001

=Bino(129,1) = 0x81 = 1000-0001

=Bino(257,1) = 0x0101 = 0000-0001|0000-0001

The function is rigged to insert Error messages into the CELL in case the number is negative or bytes > 10. Can be easily removed or changed.

It helped me while testing VBA simulating lots of math for optimization code for 8 bits microcontrollers (AVR) assembly, creating routines for Sin(x) and Ln(x) with 16 bits precision.

Public Function Bino(ByVal Valo As Double, Optional ByVal Bytes As Long = 2) As String
Dim Conta
Dim Resul as String
Dim Bits
Dim SA As Double
Dim SB As Double
Dim SX As String

If Valo < 0 Then
   Bino = "[Err: Negative]"
   Exit Function
   End If

If Bytes > 4 Then
   Bino = "[Err: Bytes > 10]"
   Exit Function
   End If

ValoHex = ""
SB = Valo
Do While SB > 1
   SA = SB / 16
   ValoHex = Hex(Int(16 * (SA - Int(SA)))) & ValoHex
   SB = SA
Loop

If Len(ValoHex) Mod 2 = 1 Then ValoHex = "0" & ValoHex
Zeroz = Bytes * 2 - Len(ValoHex)
If Zeroz = 2 Then ValoHex = "00" & ValoHex
If Zeroz = 4 Then ValoHex = "0000" & ValoHex
ValoHexLen = Len(ValoHex)
ValoHex = "0x" & ValoHex

If Bytes < ValoHexLen Then Bytes = ValoHexLen / 2
Bits = Bytes * 8 - 1
For Conta = 0 To Bits
    Div = ""
    If Conta And Conta Mod 4 = 0 Then Div = "-"
    If Conta And Conta Mod 8 = 0 Then Div = "|"
    If Int(Valo / 2) = Valo / 2 Then Bitt = 0 Else Bitt = 1
    Resul = Bitt & Div & Resul
    Valo = Int(Valo / 2)
Next Conta

Resul = ValoHex & " = " & Resul
Bino = Resul

End Function

I modified AndruWitta's modification a little bit to handle negative numbers and return the binary number in two's complement format.

Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
  DecToBin = ""
  DecimalIn = CDec(DecimalIn)
  If DecimalIn < 0 Then
  DecimalIn = -DecimalIn - 1

     Do While DecimalIn <> 0
        DecToBin = Trim$(Str$(Not DecimalIn - 2 * (Int(DecimalIn / 2 + 1)))) & DecToBin
        DecimalIn = Int(DecimalIn / 2)
    Loop
    DecToBin = Trim$(Str$(1)) & DecToBin
  Else
    Do While DecimalIn <> 0
        DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
        DecimalIn = Int(DecimalIn / 2)
    Loop
  End If
  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

Here's an Excel file which can deal with positive and negative 16 bit numbers (sint16) without VBA, see my answer here: https://stackoverflow.com/a/64261306/2738240

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top