I\'m trying to convert 4503599627370495
into binary in Excel. DEC2BIN() returns #NUM! error because DEC2BIN cannot handle such a large number.
Any thou
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
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
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
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
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